How do I convert a fixed byte array to a String in managed c++/cli ?
For example I have the following Byte array.
Byte byte_data[5];
byte_data[0]='a';
byte_data[1]='b';
byte_data[2]='c';
byte_data[3]='d';
byte_data[4]='e';
I have tried the following code
String ^mytext=System::Text::UTF8Encoding::UTF8->GetString(byte_data);
I get the following error:
error C2664: 'System::String ^System::Text::Encoding::GetString(cli::array<Type,dimension> ^)' : cannot convert parameter 1 from 'unsigned char [5]' to 'cli::array<Type,dimension> ^'
Arm yourself with some knowledge about casting between pointers to signed and unsigned types and then you should be set to use
String::String(SByte*, Int32, Int32)
. It might also pay to read the Remarks on the page, specifically around encoding.I've reproduced the sample from the page here:
Here is one option:
Not compiled but I think you get the idea.
Or use the String constructor, as indicated by @ta.speot.is, with encoding set to
System.Text::UTF8Encoding
.For those interested in another working solution. I used the notes of ta.speot.is and developed a working solution,You should be able to use this solution or that provided by Rasmus.