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);
But it just returned a weird Chinese character, which isn't the right value for c
.
We can convert a char to a string object in java by using the Character. toString() method.
This last part of the definition is important: all C-strings are char arrays, but not all char arrays are c-strings. C-strings of this form are called “string literals“: const char * str = "This is a string literal.
char is a primitive data type whereas String is a class in java. char represents a single character whereas String can have zero or more characters. So String is an array of chars.
In C, char* means a pointer to a character. Strings are an array of characters eliminated by the null character in C.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With