简体   繁体   中英

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 code

    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM