Convert ASCII string to Unicode? Windows Clean C

I found the answers to this question for many programming languages ​​other than C using the Windows API. No answers in C ++, please. Consider the following:

#include <windows.h> char *string = "The quick brown fox jumps over the lazy dog"; WCHAR unistring[strlen(string)+1]; 

What function can I use to populate unistring with characters from a string?

+4
source share
5 answers

MultiByteToWideChar :

 #include <windows.h> char *string = "The quick brown fox jumps over the lazy dog"; size_t len = strlen(string); WCHAR unistring[len + 1]; int result = MultiByteToWideChar(CP_OEMCP, 0, string, -1, unistring, len + 1); 
+9
source
+2
source

If you are really serious about Unicode, you should turn to International Unicode Components , which is a cross-platform solution for handling Unicode conversions and storing them in C or C ++.

For example, your WCHAR not Unicode, because Microsoft somewhat prematurely defined wchar_t as 16-bit (UCS-2) and got stuck in black backward compatibility when Unicode became 32-bit: UCS-2 is almost, but not quite identical to UTF-16 , the latter is actually multibyte encoding, like UTF-8. Unicode "wide" format means 32 bits (UTF-32), and even then you do not have a 1: 1 relationship between code points (i.e. 32-bit values) and abstract characters (such as a printable character).

Graphic, desired list of links:

+2
source

You can use mbstowcs to convert from "multibyte" to wide character strings.

0
source

This is another way to do this. This is not so straightforward, but when you do not want to type 6 arguments in a very specific order and remember the code numbers / macros of MultiByteToWideChar , it does the job. It takes 16 microseconds on this laptop, most of which (9 microseconds) were spent on AddAtomW .

For reference, MultiByteToWideChar takes from 0 to 1 microseconds.

 #include <Windows.h> const wchar_t msg[] = L"We did it!"; int main(int argc, char **argv) { char result[(sizeof(msg) / 2) + 1]; ATOM tmp; tmp = AddAtomW(msg); GetAtomNameA(tmp, result, sizeof(result)); MessageBoxA(NULL ,result,"it says", MB_OK | MB_ICONINFORMATION); DeleteAtom(tmp); return 0; } 
0
source

All Articles