Author Topic: Console printing erroneous Japanese characters  (Read 144 times)

Offline Shademp

  • Single posting newcomer
  • *
  • Posts: 3
Console printing erroneous Japanese characters
« on: November 07, 2018, 11:54:52 pm »
For example this set of character in the Code::Blocks IDE "燼うぬむ味厩" becomes printed as "辯シ縺・〓繧€蜻ウ蜴ゥ" in the cmd.

In Visual Studio 2017 the symbols print correctly, but I'd prefer to continue working in Code::Blocks if possible.

Code: [Select]
#include <iostream>
#include <stdio.h>
#include <cwchar>
#include <windows.h>

int main()
{

    system("chcp 932 > nul");
    cout << "燼うぬむ味厩" << endl;
    return 0;
}

I am aware of wcout but when I use that no text gets printed at all in the cmd. Given that my 6 symbols become interpreted as 12 symbols in total, it seems I need to hard-enable wide characters but so far I have failed in doing so.

Additional notes:
- File encoded in UTF-8, my program refuses to accept UTF-16 in any shape or form
- My Japanese-compatible cmd font is MS Gothic
- OS is Windows 8.1
- Visual Studio printed the text correctly whether I used cout or wcout

Thanks in advance for any suggestions on how to proceed.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 6359
    • My Best Post
Re: Console printing erroneous Japanese characters
« Reply #1 on: November 08, 2018, 12:22:38 am »
The easiest solution to use code that works with your compiler and OS.
No idea what compiler you are using; but, guessing MinGW GCC; the work around was posted for that group of compilers somewhere on this forum.
You might try searching the forum after you read the FAQs and search the wiki page.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2062
Re: Console printing erroneous Japanese characters
« Reply #2 on: November 08, 2018, 12:52:20 am »
this is probably some unicode problem. Either your file has the wrong encoding or your console, or your compiler...
Some steps you can try:
http://forums.codeblocks.org/index.php/topic,18803.msg128791.html#msg128791

or even better:
http://forums.codeblocks.org/index.php/topic,22168.msg150872.html#msg150872

Offline Shademp

  • Single posting newcomer
  • *
  • Posts: 3
Re: Console printing erroneous Japanese characters
« Reply #3 on: November 08, 2018, 11:56:29 pm »
My apologies for posting this in the wrong section. Had I read the forum instructions in more detail I would have known to post this newbie question in the "Using Code::Blocks" section instead. Feel free to move or delete this thread.

Thank you Tim for pointing me in the direction of compilers. I would have not considered that to be the issue otherwise and now I appreciate the difference between Code::Blocks and compilers. I am using the mingw32-g++ compiler that came packaged with my C::B installation. To confirm 100% that my issue is compiler-related, I installed MinGW properly and ran my program via that path, no C::B involved.

Indeed, the same garbled text got printed even without the C::B. The only difference is that the erroneous symbol became a padding space. While my newly installed MinGW compiler appears to be from 2017, with the compiler in the C::B package being from 2015, I am still assuming that my issue is indeed the compiler.


Searching for the solution on the forum has proven fruitless on my end however. I've tried searching using keywords like "unicode", "compiler", "encoding", "wide character" etc but nothing presents itself to me as the solution. Granted I could be reading the solution and still not know it's there, due to how new I am to the world of programming.

The closest that my eyes signal as potentially related to the solution are these wiki pages:
- Unicode Standards
- Code::Blocks variable types synthesis

There is a lot of talk about this "wxWidgets" thing all over the forum. Is it recommended that I install that and learn how it works? Will that bring me closer to the solution?

I shall not try the patience of the forum beyond this post but once again I appreciate any pointers in the right direction. Thanks in advance.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 6359
    • My Best Post
Re: Console printing erroneous Japanese characters
« Reply #4 on: November 09, 2018, 12:13:46 am »
this is probably some unicode problem. Either your file has the wrong encoding or your console, or your compiler...
Some steps you can try:
http://forums.codeblocks.org/index.php/topic,18803.msg128791.html#msg128791

or even better:
http://forums.codeblocks.org/index.php/topic,22168.msg150872.html#msg150872

The solution was posted already in this thread!
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Shademp

  • Single posting newcomer
  • *
  • Posts: 3
Re: Console printing erroneous Japanese characters
« Reply #5 on: November 09, 2018, 11:56:52 am »
My most sincere apologies for my absent-mindedness. Since I originally had so much difficulty to make Japanese characters be displayed at all (taking a full day to find where to change the System Language setting), I had completely moved away from the "chcp 65001" solution (since it had never worked for me before I figured out about the System Language settings thing) and stayed strictly to the "chcp 932" code, hard-assuming out of stupidity that I HAD to stick to the latter. This was obviously not the case, as I now rewrote the code to reference chcp 65001 instead and indeed now the characters are displayed correctly.

If it's any consolation, I have in fact learned something even though I had to be pointed straight to the solution numerous times. You may mark this thread as "Resolved". Thank you for your help. I shall not be a bother again.