A customer had an LCID and wanted to know what the code page is for that locale. For example, given locale 1033 (US-English), it should report that the code page is 1252 (Windows Latin 1). They need this information because the file format uses ANSI strings, and the file format for some reason doesn’t provide a code page, but does provide a locale.
You can ask GetLocaleInfo
for the LOCALE_IDEFAULTANSICODEPAGE
to get the ANSI code page for a locale.
UINT GetAnsiCodePageForLocale(LCID lcid) { UINT acp; int sizeInChars = sizeof(acp) / sizeof(TCHAR); if (GetLocaleInfo(lcid, LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER, reinterpret_cast<LPTSTR>(&acp), sizeInChars) != sizeInChars) { // Oops - something went wrong } return acp; }
This function uses the LOCALE_RETURN_NUMBER
flag to say, “Hey, I know that the GetLocaleInfo
function normally returns strings, and that’s great, but we both know that this thing I’m asking for is an integer (because the name beings with an I
). Officially, you need to take that integer and convert it to a string, and officially I need to take that string and convert it back to an integer. How about let’s talk like people and you just give me the integer directly?”
And even though you didn’t ask, you can use LOCALE_IDEFAULTCODEPAGE
to get the OEM code page for a locale.
Bonus gotcha: There are a number of locales that are Unicode-only. If you ask the GetLocaleInfo
function and ask for their ANSI and OEM code pages, the answer is “Um, I don’t have one.” (You get zero back.)
0 comments