Skip to content

Instantly share code, notes, and snippets.

@taoky
Created November 10, 2024 13:31
Show Gist options
  • Save taoky/48c4a892c68b615153ef863be70e1574 to your computer and use it in GitHub Desktop.
Save taoky/48c4a892c68b615153ef863be70e1574 to your computer and use it in GitHub Desktop.
Wide character compatibility

Wide Character Compatibility on Windows, WINE and POSIX

Standard C

#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main() {
    setlocale(LC_ALL, "");

    wchar_t buffer[256];
    fgetws(buffer, 256, stdin);

    for (int i = 0; buffer[i] != L'\0'; i++) {
        wchar_t wc = buffer[i];
        unsigned char *bytes = (unsigned char*) &wc;
        int size = sizeof(wchar_t);

        wprintf(L"Character %c in hexadecimal: ", wc);
        for (int j = 0; j < size; j++) {
            wprintf(L"%02x ", bytes[j]);
        }
        wprintf(L"\n");
    }

    return 0;
}

MSVC (cl)

Windows CP936

测试abc
Character 测 in hexadecimal: 4b 6d
Character 试 in hexadecimal: d5 8b
Character a in hexadecimal: 61 00
Character b in hexadecimal: 62 00
Character c in hexadecimal: 63 00
Character
 in hexadecimal: 0a 00

Windows CP65001

测试abc
Character 娴 in hexadecimal: 34 5a
Character 嬭 in hexadecimal: 2d 5b
Character 瘯 in hexadecimal: 2f 76
Character a in hexadecimal: 61 00
Character b in hexadecimal: 62 00
Character c in hexadecimal: 63 00
Character
 in hexadecimal: 0a 00

MinGW (MSYS2)

Windows CP936

测试abc
Character K in hexadecimal: 4b 6d
Character  in hexadecimal: d5 8b
Character a in hexadecimal: 61 00
Character b in hexadecimal: 62 00
Character c in hexadecimal: 63 00
Character
 in hexadecimal: 0a 00

Windows CP65001

测试abc
Character 4 in hexadecimal: 34 5a
Character - in hexadecimal: 2d 5b
Character / in hexadecimal: 2f 76
Character a in hexadecimal: 61 00
Character b in hexadecimal: 62 00
Character c in hexadecimal: 63 00
Character
 in hexadecimal: 0a 00

Wine Running MSVC & MSYS2 MinGW

Type in:

测试abc
Character ? in hexadecimal: 3f 00
Character ? in hexadecimal: 3f 00
Character a in hexadecimal: 61 00
Character b in hexadecimal: 62 00
Character c in hexadecimal: 63 00
Character
 in hexadecimal: 0a 00

Pipe:

$ echo 测试abc | wine wide-mingw.exe
Character æ in hexadecimal: e6 00
Character µ in hexadecimal: b5 00
Character 9 in hexadecimal: 39 20
Character è in hexadecimal: e8 00
Character ¯ in hexadecimal: af 00
Character " in hexadecimal: 22 20
Character a in hexadecimal: 61 00
Character b in hexadecimal: 62 00
Character c in hexadecimal: 63 00
Character
 in hexadecimal: 0a 00

POSIX

测试abc
Character K in hexadecimal: 4b 6d 00 00 
Character  in hexadecimal: d5 8b 00 00 
Character a in hexadecimal: 61 00 00 00 
Character b in hexadecimal: 62 00 00 00 
Character c in hexadecimal: 63 00 00 00 
Character 
 in hexadecimal: 0a 00 00 00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment