How to use UTF8Encoding in Visual C++
I need to change the below c# code to c++ code.
public static byte[] StrToByteArray(string str)
{
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetBytes(str);
}
on this website i found the c++ code for UTF8Encoding from which i created this c开发者_JAVA百科ode
void StrToByteArray(string unicodeString)
{
UTF8Encoding^ utf8 = gcnew UTF8Encoding;
array<Byte>^encodedBytes = utf8->GetBytes( unicodeString );
}
but this gives me the following error
Error 2 error C2664: 'cli::array ^System::Text::Encoding::GetBytes(cli::array ^)' : cannot convert parameter 1 from 'std::string' to 'cli::array
Why would it do this while it is identical to the documentation? (except i am using a normal string, but using a top level string^ gives me an error on that.)
i'm not sure if it is related but my code is managed.
note: i tried not worrying yet about returning any data till i get this working.
string
is a different data type in C++ as it is in C#. Try using System::String^
instead.
精彩评论