2

In my MFC application I am reading Japanese characters from string table then converting it into multibyte using following code

WCHAR wBuf[1024];
int rc;

rc = LoadStringW(hInstance, iResourceID, wBuf, 1024);

WideCharToMultiByte(1252, WC_COMPOSITECHECK, wBuf, -1, buf, 1024, NULL, NULL);

But every Japanese character is converted into '????' I tried to change the codepage from 1252 to 1200 but no help.

Makoto
  • 104,088
  • 27
  • 192
  • 230
Rahul
  • 1,401
  • 4
  • 17
  • 33
  • Are you sure that you store the string in the RC file in the correct encoding? – Vinzenz Oct 12 '11 at 07:38
  • Yes. I saved RC file using VS 2010 'Advanced Save Options'->'Unicode - Codepage 1200', I tried using 1200 codepage '1200' in WideCharToMultiByte but still no go. – Rahul Oct 12 '11 at 07:41
  • Michael says 1200 is not a valid codepage for C++. That might be part of the problem. – Mooing Duck Apr 29 '12 at 19:51

2 Answers2

5

Windows-1258 is the code page for Vietnamese text. Japanese cannot be expressed in the Vietnamese code page, so the output is mapped to question marks. The same goes for 1252, it's only for West European languages.

In the case of 1200, that's not a real code page: according to MSDN, it's only available to managed applications (i.e. .NET).

I'd strongly suggest just working with the Unicode directly, but if you absolutely must convert this to a multibyte character set, you'll need one that supports Japanese, in which case Shift-JIS, code page 932, is the usual code page.

Michael Madsen
  • 54,231
  • 8
  • 72
  • 83
  • 1
    @Rahul: Then there are two options: either the input isn't wide chars as you expect (which suggests a problem with your resource file), or it's not Japanese (which means you need a different code page). Without seeing the string, I can't tell you which of the two it is. – Michael Madsen Oct 12 '11 at 08:04
  • 必要条件 This Japanese string I am trying to convert. wBuf contains the correct Japanese string after reading it from the resource file. '???' characters appear after call to WideCharToMultiByte – Rahul Oct 12 '11 at 08:55
1

Yes. I saved RC file using VS 2010 'Advanced Save Options'->'Unicode - Codepage 1200', I tried using 1200 codepage '1200' in WideCharToMultiByte but still no go.

Well that's only doing partly the trick actually you need to specify the encoding for the data in the .rc file like this:

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_JPN)
#ifdef _WIN32
LANGUAGE LANG_JAPANESE,SUBLANG_JAPANESE_JAPAN
#pragma code_page(932)
#endif

STRINGTABLE
BEGIN
   STR_ID "<Japanese text goes here>"
END

#endif
Vinzenz
  • 2,749
  • 17
  • 23
  • Of course this might also work via the resource editor - However you need to ensure that the data is structured the right way within the rc file. – Vinzenz Oct 12 '11 at 07:51