views:

87

answers:

3
printf("%s\n", multibytestring);

By default the multi-byte characters will show up like ??? in console, how can I fix it?

A: 

try wprintf("%s\n", multibytestring);

Anders K.
A: 

You want "%ls" for wide-character strings. Is that what you're after?

Here's a more complete answer:

  • You can use wprintf to output 16-bit strings by default using "%s"
  • You can use "%ls" or "%hs" to explicitly specify the char/string width for individual arguments, regardless of printf variant
  • See: http://msdn.microsoft.com/en-us/library/56e442dc.aspx for MS's reference docs

If you're asking about UTF8 characters not appearing right, it's probably platform-specific (dependent on the codepage/handling for the console). You should be able to output UTF8 as ASCII, but the display will have to be able to handle UTF8 correctly, and you may need to set the appropriate codepage or other environment setting (not too sure about this, I think it's app/platform specific).

Nick
A: 

I'm guessing Windows, and that you mean multi-byte characters and not wide characters.

Make sure that _MBCS is defined. Try calling setlocale and then _setmbcp:

setlocale(LC_ALL, "japanese");
_setmbcp(_MB_CP_LOCALE);

After that it should hopefully work fine.

dalle
seems `_setmbcp` is not necessary.
Alan
BTW,is there a SINGLE locale that can be used to show all characters world wide?
Alan
@Alan: That would be Unicode. Unfortunately Windows doesn't support UTF-8 locale.
dalle
Doesn't it support UTF-16?
Roman A. Taycher
@Roman A. Taycher: Windows does indeed support UTF-16, but only in the form of `wchar_t`/wide strings.
dalle