Is it possible to assume that all Windows platforms will be in UCS-2 LE

I am attaching a text file to my project, adding it to the resource, and then uploading it.

I use LockResource and static_cast to pass it to std::wstring
std::wstring sData(static_cast<wchar_t*>(pData));

My project uses UNICODE (windows), so I use std::wstring and wchar_t .

I found out that I need to set the encoding in the UCS-2 LE file, otherwise she would just read gibberish. I guess this is because this is what Windows uses.

My question is, can I assume that all Windows operating systems currently use UCS-2 LE? I do not want to start the system using UCS-2 BE (or something else). My program would be terribly terrible.

I could save the file in ANSI and then convert it to the encoding used by the operating system using MultiByteToWideChar , but that would be a waste of time if it would definitely be UCS-2 LE.

+4
source share
2 answers

All recent and latest versions of Windows (excluding XBox) use UTF-16 LE.

Note that there is an error with the way you initialize the string variable:

 std::wstring sData(static_cast<wchar_t*>(pData)); 

This assumes that the resource ends with a final (double-byte) 0, which, I think, is not guaranteed if you simply refer to the file in your resources. You should get the size of the resource and use a constructor with two pointers for sData.

If you are worried about time (as suggested by your comment on using MultiByteToWideChar ), you should know that you are copying data from a resource into dynamic memory, and this copy is probably almost as slow as doing the conversion. If you do this only once, I will not worry about speed. I would save the text as UTF-8 and use MultiByteToWideChar , especially if the UTF-8 encoding is more efficient for your text, as this will reduce your binary.

If speed is a problem (and if you don't need to change the line at run time), I would not use std::wstring . I would make a class that provides a similar interface, but should it point directly to the resource memory, and not copy all the text into dynamic memory. This saves load time and memory.

+5
source

All versions of LE windows, and I don’t think Microsoft has a plan to change its OS to BE. and Windows NT 5 (Win2K), and then all are based on UTF-16, so Yes, it’s always safe to assume that UCS-2 LE windows

0
source

All Articles