Why doesn't printf format unicode parameters?

前端 未结 2 1992
[愿得一人]
[愿得一人] 2020-12-09 21:33

When using printf to format a double-byte string into a single-byte string:

printf(\"%ls\\n\", L\"s:\\\\яшертыHello\");   // %ls for a wide string (%s varies         


        
相关标签:
2条回答
  • I expect your code to work -- and it works here on Linux -- but it is locale dependent. That means you have to set up the locale and your locale must support the character set used. Here is my test program:

    #include <locale.h>
    #include <stdio.h>
    
    int main()
    {
        int c;
        char* l = setlocale(LC_ALL, "");
        if (l == NULL) {
            printf("Locale not set\n");
        } else {
            printf("Locale set to %s\n", l);
        }
        printf("%ls\n", L"s:\\яшертыHello");
        return 0;
    }
    

    and here is an execution trace:

    $ env LC_ALL=en_US.utf8 ./a.out
    Locale set to en_US.utf8
    s:\яшертыHello
    

    If it says that the locale isn't set or is set to "C", it is normal that you don't get the result you expect.

    Edit: see the answers to this question for the equivalent of en_US.utf8 for Windows.

    0 讨论(0)
  • 2020-12-09 22:00

    In C++ I usually use std::stringstream to create formatted text. I also implemented an own operator to use Windows function to make the encoding:

    ostream & operator << ( ostream &os, const wchar_t * str )
    {
      if ( ( str == 0 ) || ( str[0] == L'\0' ) )
       return os;
      int new_size = WideCharToMultiByte( CP_UTF8, 0, str, -1, NULL, NULL, NULL, NULL );
      if ( new_size <= 0 )
        return os;
      std::vector<char> buffer(new_size);
      if ( WideCharToMultiByte( CP_UTF8, 0, str, -1, &buffer[0], new_size, NULL, NULL ) > 0 )
        os << &buffer[0];
      return os;
    }
    

    This code convert to UTF-8. For other possibilities check: WideCharToMultiByte.

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