C++ cli 在UTF8中将System::String转换为std::String,随后将其转换为c_str格式的char* 我有一个系统::C++代码中的字符串^ < /COD>变量。此变量应转换为std::string,然后通过c_str转换为const char* // original string System::String^ path = ...; // convert to std::string msclr::interop::marshal_context context; std::string filename(context.marshal_as<std::string>(path)); // call API function that internally connects to sqlite3 using sqlite3_open as // sqlite3_open(filename.c_str()) // https://www.sqlite.org/c3ref/open.html - // const char *filename, /* Database filename (UTF-8) */ doCalculation(filename) //原始字符串 系统::字符串^path=。。。; //转换为std::string msclr::interop::封送上下文; 字符串文件名(context.marshal_as(path)); //使用sqlite3_open as调用内部连接到sqlite3的API函数 //sqlite3_open(filename.c_str()) // https://www.sqlite.org/c3ref/open.html - //常量字符*文件名,/*数据库文件名(UTF-8)*/ 文件计算(文件名)

C++ cli 在UTF8中将System::String转换为std::String,随后将其转换为c_str格式的char* 我有一个系统::C++代码中的字符串^ < /COD>变量。此变量应转换为std::string,然后通过c_str转换为const char* // original string System::String^ path = ...; // convert to std::string msclr::interop::marshal_context context; std::string filename(context.marshal_as<std::string>(path)); // call API function that internally connects to sqlite3 using sqlite3_open as // sqlite3_open(filename.c_str()) // https://www.sqlite.org/c3ref/open.html - // const char *filename, /* Database filename (UTF-8) */ doCalculation(filename) //原始字符串 系统::字符串^path=。。。; //转换为std::string msclr::interop::封送上下文; 字符串文件名(context.marshal_as(path)); //使用sqlite3_open as调用内部连接到sqlite3的API函数 //sqlite3_open(filename.c_str()) // https://www.sqlite.org/c3ref/open.html - //常量字符*文件名,/*数据库文件名(UTF-8)*/ 文件计算(文件名),c++-cli,C++ Cli,它适用于ASCII路径,但如果路径包含非拉丁字符,则会失败 因此,我需要将封送std::string从当前实现(ASCII?)转换为UTF8 我试过了 std::wstring dbPath(context.marshal_as<std::wstring>(path)); std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> convert; std::strin

它适用于ASCII路径,但如果路径包含非拉丁字符,则会失败

因此,我需要将封送std::string从当前实现(ASCII?)转换为UTF8

我试过了

    std::wstring dbPath(context.marshal_as<std::wstring>(path));
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> convert;
    std::string dbPathU8 = convert.to_bytes(dbPath);
std::wstring dbPath(context.marshal_as(path));
std::wstring_convert;
std::string dbPathU8=转换为_字节(dbPath);

但是它不起作用。

您要做的是使用.Net方法直接转换为UTF-8

Encoding类中的可用方法并不完全是您想要的(直接从托管字符串到非托管字符串或字节数组),因此我们需要一个中介和一些手动复制

String^ path = ...;

// First, convert to a managed array of the bytes you want.
array<Byte>^ bytes = Encoding::UTF8->GetBytes(path);

// Then, copy those bytes from the managed byte array to an unmanaged string.
std::string str;
str.resize(bytes->Length);
Marshal::Copy(bytes, 0, IntPtr(str.data()), bytes->Length);

// OR, copy directly to the char* you want eventually.
char* chars = new char[bytes->Length + 1]; // or malloc(), or whatever.
Marshal::Copy(bytes, 0, IntPtr(chars), bytes->Length);
chars[bytes->Length] = '\0'; // null terminate.
// don't forget to free the buffer when you're done with it!
String^path=。。。;
//首先,转换为所需字节的托管数组。
数组^bytes=编码::UTF8->GetBytes(路径);
//然后,将这些字节从托管字节数组复制到非托管字符串。
std::字符串str;
str.resize(字节->长度);
封送:复制(字节,0,IntPtr(str.data()),字节->长度);
//或者,直接复制到您最终想要的字符*中。
char*chars=新字符[字节->长度+1];//或者malloc(),或者别的什么。
封送:复制(字节,0,IntPtr(字符),字节->长度);
字符[字节->长度]='\0';//空终止。
//完成缓冲后,别忘了释放缓冲区!

有几种变体可用,但它们的参数似乎要么都是托管的,要么都是非托管的。(
String^
array^
,或
char*
byte*
,但不是String^和byte*)因此,我们让编码类创建一个托管字节数组,然后使用该方法将这些字节复制到非托管字符串对象,或者直接转换为字符*

您要做的是使用.Net方法直接转换为UTF-8

Encoding类中的可用方法并不完全是您想要的(直接从托管字符串到非托管字符串或字节数组),因此我们需要一个中介和一些手动复制

String^ path = ...;

// First, convert to a managed array of the bytes you want.
array<Byte>^ bytes = Encoding::UTF8->GetBytes(path);

// Then, copy those bytes from the managed byte array to an unmanaged string.
std::string str;
str.resize(bytes->Length);
Marshal::Copy(bytes, 0, IntPtr(str.data()), bytes->Length);

// OR, copy directly to the char* you want eventually.
char* chars = new char[bytes->Length + 1]; // or malloc(), or whatever.
Marshal::Copy(bytes, 0, IntPtr(chars), bytes->Length);
chars[bytes->Length] = '\0'; // null terminate.
// don't forget to free the buffer when you're done with it!
String^path=。。。;
//首先,转换为所需字节的托管数组。
数组^bytes=编码::UTF8->GetBytes(路径);
//然后,将这些字节从托管字节数组复制到非托管字符串。
std::字符串str;
str.resize(字节->长度);
封送:复制(字节,0,IntPtr(str.data()),字节->长度);
//或者,直接复制到您最终想要的字符*中。
char*chars=新字符[字节->长度+1];//或者malloc(),或者别的什么。
封送:复制(字节,0,IntPtr(字符),字节->长度);
字符[字节->长度]='\0';//空终止。
//完成缓冲后,别忘了释放缓冲区!

有几种变体可用,但它们的参数似乎要么都是托管的,要么都是非托管的。(代码>字符串Stry和代码>数组^ ,或<代码> char *< /COD>和字节*>代码>,但不是string ^和字节*.),我们有编码类创建托管字节数组,然后我们使用该方法将这些字节复制到非托管字符串对象,或者直接复制到char *.< /p>这不是C++,C++是C++的C++,我用C++ C++来CLIG/CLI检索字符串的UTF-8字节数组,然后转换成<代码> STD::String < /C> >,这不是C++,它是一个奇怪的微软ISM,叫做C++,CLI——我已经为你修好了标签。为什么不使用C++ +CLI来为字符串检索UTF 8字节数组,并将其转换为
std::string
?对于第一个调用
marshall.Copy
I接收到错误C2440::“const char*”无法转换为“System::IntPtr”,第二个选项有效。之后,我用
std::string(chars)
将数组复制到一个新字符串中,感谢您发布此消息!对于第一个调用
Marshal.Copy
I接收到错误C2440::“const char*”无法转换为“System::IntPtr”,第二个选项有效。之后,我用
std::string(chars)
将数组复制到一个新字符串中,感谢您发布此消息!