char *转换为C#中的字符串 [英] char* to a string in C#
本文介绍了char *转换为C#中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从本机DLL调用一个返回char*
指针的函数,如何将返回的指针转换为字符串?
我试过了:
I'm calling a function from a native DLL which returns a char*
pointer, how can I convert the returned pointer to a string ?
I tried :
char* c = function();
string s = new string(c);
但是它只是返回了一个奇怪的汉字,这不是c
的正确值.
But it just returned a weird Chinese character, which isn't the right value for c
.
推荐答案
也许本机DLL实际上返回的是ANSI字符串而不是Unicode字符串.在这种情况下,请致电Marshal.PtrToStringAnsi
:
Perhaps the native DLL is actually returning an ANSI string instead of a Unicode string. In that case, call Marshal.PtrToStringAnsi
:
using System.Runtime.InteropServices;
...
string s = Marshal.PtrToStringAnsi((IntPtr)c);
这篇关于char *转换为C#中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文