Converting unicode strings and vice-versa

前端 未结 6 834
谎友^
谎友^ 2020-12-05 03:05

I\'m kind of new to using Unicode string and pointers and I\'ve no idea how the conversion to unicode to ascii and versa-versa works. Following is what I\'m trying

相关标签:
6条回答
  • 2020-12-05 03:56

    In the future (VS 2010 already supports it), this will be possible in standard C++ (finally!):

    #include <string>
    #include <locale>
    
    std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
    const std::wstring wide_string = L"This is a string";
    const std::string utf8_string = converter.to_bytes(wide_string);
    
    0 讨论(0)
  • 2020-12-05 04:03

    The widen() algorithm converts char to wchar_t :

    char a;
    a = 'a';
    whcar_t wa = cin.widen(a);
    

    Of course, you have to put it into a loop. And resolve the *; The opposite is accomplished by narrow()

    0 讨论(0)
  • 2020-12-05 04:05

    The solutions are platform-dependent. On Windows use MultiByteToWideChar and WideCharToMultiByte API functions. On Unix/linux platforms iconv library is quite popular.

    0 讨论(0)
  • 2020-12-05 04:05

    The conversion from ASCII to Unicode and vice versa are quite trivial. By design, the first 128 Unicode values are the same as ASCII (in fact, the first 256 are equal to ISO-8859-1).

    So the following code works on systems where char is ASCII and wchar_t is Unicode:

    const char* ASCII = "Hello, world";
    std::wstring Unicode(ASCII, ASCII+strlen(ASCII));
    

    You can't reverse it this simple: 汉 does exist in Unicode but not in ASCII, so how would you "convert" it?

    0 讨论(0)
  • 2020-12-05 04:05

    C++ by itself doesn't offer this functionality. You'll need a separate library, like libiconv.

    0 讨论(0)
  • 2020-12-05 04:10

    C Standard library functions: mbstowcs and wcstombs

    0 讨论(0)
提交回复
热议问题