October 7th, 2016

How can I get the default code page for a locale?

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 Get­Locale­Info for the LOCALE_IDEFAULT­ANSI­CODE­PAGE 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 Get­Locale­Info 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_IDEFAULT­CODE­PAGE 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 Get­Locale­Info function and ask for their ANSI and OEM code pages, the answer is “Um, I don’t have one.” (You get zero back.)

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.