简体   繁体   English

非ASCII字符-从std :: string转换为char *

[英]Non-ASCII characters - conversion from std::string to char*

I have a string with non-ASCII characters, for example std::string word ("żółć"); 我有一个包含非ASCII字符的字符串,例如std::string word ("żółć"); or std::string word ("łyżwy"); std::string word ("łyżwy"); I need to convert it properly to const char * in order to call system(my_String_As_A_Const_Char_Pointer); 我需要将其正确转换为const char *才能调用system(my_String_As_A_Const_Char_Pointer);

I'm working on Linux. 我正在使用Linux。

How can I do it? 我该怎么做?

You can use the std::string::c_str member function. 您可以使用std::string::c_str成员函数。 It will return a const char * that can be used in functions that accept that type of argument. 它将返回一个const char * ,可以在接受该类型参数的函数中使用。 Here's an example: 这是一个例子:

int main(int, char*[]) {
    std::string word("żółć");
    const char* x = word.c_str();
    std::cout << x;
}

And here 'sa live example. 在这里 “SA活生生的例子。

With these conversions the only thing to care about is mixing wide chars with normal chars (which fails horribly). 通过这些转换,唯一要关心的是将宽字符与普通字符混合(这会严重失败)。 You are using a std:string, so c_str() is fine for pulling out a const char* to pass to some other library call. 您使用的是std:string,因此c_str()适合提取const char *传递给其他库调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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