0

I have a program in which I have been printing normal ASCII strings on screen using TextOut(). I now want to add the capability of printing out Shift-JIS encoded strings. Can I somehow tell TextOut() that I want to print a Shift-JIS string or do I have to use another function altogether? The documentation for TextOut seems to make no mention of encoding.

FYI: My program is currently compiled with MS visual studio 2015 and the "Character Set" is set to "Use Multi-Byte Character Set".

Mick
  • 8,284
  • 22
  • 81
  • 173
  • You will need to learn how the Windows API handles encodings. The process that runs has an "ANSI" encoding, which IIRC is decided by the specific installation of Windows your program is being run on and can be anything (including Shift-JIS), and a "Unicode" or "Wide" encoding, which is UTF-16. What you want to do is convert your Shift-JIS to UTF-16 using `MultiByteToWideChar()` and then using `TextOutW()` to draw the UTF-16 string. This will work on any Windows system regardless of configuration. – andlabs Apr 22 '16 at 16:16
  • I want the resulting exe to work on my own English set up Windows PC as well as a Japanese PC being used in Japan. Are you telling me I need to do things differently depending on how the PC is set up? – Mick Apr 22 '16 at 16:46
  • 1
    You will if you stick with the multibyte character set option. Convert to and from Unicode and use modern Windows APIs that require Unicode (functions with `W` suffixes and both `#define UNICODE` and `#define _UNICODE`, and the requisite VS project option) and your life will be easier. `MultiByteToWideChar()` and `WideCharToMultiByte()` both support Shift-JIS by passing in 932 as the code page. (There doesn't seem to be named constants for these...?) – andlabs Apr 22 '16 at 17:35
  • (Also if you're worried about the overhead of converting back and forth all the time, I will say that the cost of the conversions seems to be negligible, at least in my case with UTF-8. Try it and see; if there is slowdown, use instrumentation to be sure of the cause.) – andlabs Apr 22 '16 at 23:09

1 Answers1

1

Thanks to andlabs, here's the complete answer. This works when the program is compiled with "Character Set" set to "Use Multi-Byte Character Set". I did not want to compile with the "character set" set to unicode as this would disrupt too much existing code.

    char shift_jis_string[MAX_STR_LEN]; // null terminated

    // blah blah, setting shift_jis_string

    WCHAR unicode_string[MAX_STR_LEN];

    int n = MultiByteToWideChar(932,0,shift_jis_string,-1,unicode_string,MAX_STR_LEN);

    TextOutW(hdc,X,Y, unicode_string, n); // note the W on the end
Mick
  • 8,284
  • 22
  • 81
  • 173