简体   繁体   English

C ++:将char *转换为wstring

[英]C++: convert char * to wstring

Arguments in argv[] are UTF-8 encoded. argv[]中的参数是UTF-8编码的。 I would like to do something like: 我想做的事情如下:

#include <wstring>
#include <???>

void doWhatever(wstring &ws);

using ???;

int main(int argc, char *argv[])
{
    while (--argc)
    {
        // Convert argv to wstring
        wstring ws = ???(argv[argc]);
        doWhatever(ws);
    }
    return EXIT_SUCCESS;
}

The ??? ??? I don't know. 我不知道。 I'm sure this is trivial for C++ people, but searching it just brings up a lot of noise. 我确信这对C ++人来说是微不足道的,但搜索它只会带来很多噪音。

That a very good question! 那个问题非常好! :-) :-)

  1. As Maxim wrote: mbstowcs() 正如Maxim所写:mbstowcs()

  2. wsprintf() with "%S" (Capital "S"). 带有“%S”(大写字母“S”)的wsprintf()。 In wsprintf() "S" means multi-byte string (in sprintf() "S" means wide-char). 在wsprintf()中,“S”表示多字节字符串(在sprintf()中,“S”表示宽字符)。

  3. You can use std::wstring_convert and choose the UTF-8 encoding. 您可以使用std :: wstring_convert并选择UTF-8编码。 I THINK its "codecvt_utf8_utf16" 我认为它的“codecvt_utf8_utf16”

For windows: 对于Windows:

  1. MultiByteToWideChar() in WINAPI WINAPI中的MultiByteToWideChar()

  2. If you set to the clipboard using SetClipboardData() the ASCII text using CF_TEXT, windows allows you to GetClipboardData() for CF_UNICODETEXT doing the conversion for you! 如果使用SetClipboardData()使用CF_TEXT将ASCII文本设置为剪贴板,则windows允许您使用GetClipboardData()为CF_UNICODETEXT进行转换!

You can also do it hardcore manually (and work only in some of the cases) by adding "NULLs" between 2 ASCII characters. 您也可以通过在2个ASCII字符之间添加“NULL”来手动执行硬核(并且仅在某些情况下工作)。

That's all comes to mind right now :-) 这就是现在想到的:-)

On UNIX/Linux use iconv . 在UNIX / Linux上使用iconv

On Windows use mbstowcs . 在Windows上使用mbstowcs

There is also a standard C++ mbstowcs , but its interface is a bit lacking. 还有一个标准的C ++ mbstowcs ,但它的界面有点缺乏。

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

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