[C++ to C#] -> Convert a char* to string  
Author Message
JosepMola





PostPosted: Visual C# General, [C++ to C#] -> Convert a char* to string Top

Hi,
I've an external library with a function that returns a struct

struct in C++:
typedef struct{
float pintura;
float sustrato;
float adherencia;
char* codiErrorAdMalla;
unsigned char * ImagenM;
int alto;
int ancho;
}DatosAdMalla;

I've the equivalent struct in C#:
public struct DatosAdMalla{
public float pintura;
public float sustrato;
public float adherencia;
public unsafe char* codiErrorAdMalla;
public unsafe byte* ImagenM;
public int alto;
public int ancho;
}

First I tried to declare 'string codiErrorAdMalla' but then the function returns me a Pinvoke error!!!

Now I want to convert 'unsafe char* codiErrorAdMalla' to 'String' but I don't know how to make it


(Sorry for my english!!)


Visual C#15  
 
 
shieldy





PostPosted: Visual C# General, [C++ to C#] -> Convert a char* to string Top

this maybe help you

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=985087&SiteID=1



 
 
JosepMola





PostPosted: Visual C# General, [C++ to C#] -> Convert a char* to string Top

this maybe help you

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=985087&SiteID=1



Thanks for your reply but I've another problem.

The struct contains a char* no char[] and I can't convert it to String with the method in the post you said me

 
 
Mattias Sjogren





PostPosted: Visual C# General, [C++ to C#] -> Convert a char* to string Top

Well you can simply do

string s = new string(codeErrorAdMalla);

But using a char* is incorrect since a .NET char represents a wide character (wchar_t in C++). So I recomment changing the field type to either a sbyte* and use the String constructor like above, or an IntPtr and use Marshal.PtrToStringAnsi.



 
 
James Curran





PostPosted: Visual C# General, [C++ to C#] -> Convert a char* to string Top

What I believe you want is:

[MarshalAs(UnmanagedType.LPStr)]
public string codiErrorAdMalla;

However, I think the real problem will be ImagenM, which I think would like to be defined as:

[MashalAs(UnmanagedType.LPArray, SizeConst=alto * ancho)]
byte[] ImagenM;

Except it will not accept "alto * ancho" as a SizeConst.



 
 
Evan Mulawski





PostPosted: Visual C# General, [C++ to C#] -> Convert a char* to string Top

Have you tried Convert.ToString(char* value) Or am I not getting your question

 
 
JosepMola





PostPosted: Visual C# General, [C++ to C#] -> Convert a char* to string Top

Have you tried Convert.ToString(char* value) Or am I not getting your question


I tried it but I only obtains the first char, not the all chars of the string!!

 
 
Evan Mulawski





PostPosted: Visual C# General, [C++ to C#] -> Convert a char* to string Top

A char* is only one letter or digit. A string is mulitple char* combined.

 
 
JThiloR





PostPosted: Visual C# General, [C++ to C#] -> Convert a char* to string Top

Consider the following:

In C++ a char is ASCII (8 Bit), in C# you work with Unicode (normally 16 Bit). Because of this, try the following: Use in the C# struct byte[].

To convert your Unicode (C#) to byte[] (in ASCII), use

Encoding.ASCII.GetBytes(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(str)); where str is your string you want to convert.


 
 
Evan Mulawski





PostPosted: Visual C# General, [C++ to C#] -> Convert a char* to string Top

Try this:

CharConverter cc = new CharConverter();

cc.ConvertToString(object value);

For the value use your char* value.



 
 
Fernando Nicolet





PostPosted: Visual C# General, [C++ to C#] -> Convert a char* to string Top

There are possibly 1000 easier (and better) ways of doing this... but I'm pretty sure this works:

 


string strCodiErrorAdMalla = string.Empty;
unsafe

{
   while (codiErrorAdMalla[0] != 0)
   {
      strCodiErrorAdMalla += codiErrorAdMalla[0].ToString();
      codiErrorAdMalla += 1;
   }
}

 

Hope it helps,

Fernando