开发者

WideCharToMultiByte when is lpUsedDefaultChar true?

I am trying to understand WideCharToMultiByte and I was wondering when lpUsedDefaultChar would be set to be TRUE.

Here is a sample: What should be lpszW inorder for the flag to be 开发者_Python百科set to be true?

lpszW = L”__WHAT SHOULD_BE_HERE__”;
int c = ??;
BOOL fUsedDefaultChar = false;
DWORD dwSize = WideCharToMultiByte(CP_ACP, 0, lpszW,  c, myOutStr ,myOutLen, NULL, &fUsedDefaultChar); 

http://msdn.microsoft.com/en-us/library/dd374130(VS.85).aspx

Any books/tutorials for understanding Unicode/UTF stuff would be great.

Thanks!


Anything that is not present in the current codepage will map to ? (by default) and UsedDefaultChar will be != FALSE.

Windows-1252 is probably the most common codepage and most of those characters map to the same value in unicode.

Take Ω (ohm) for example, it is probably not present in whatever your current codepage is and therefore will not map to a valid narrow character:

BOOL fUsedDefaultChar=FALSE;
DWORD dwSize;
char myOutStr[MAX_PATH];
WCHAR lpszW[10]=L"Hello";
*lpszW=0x2126; //ohm sign, you could also use the \u2126 syntax if your compiler supports it.
dwSize = WideCharToMultiByte(CP_ACP, 0, lpszW,  -1, myOutStr ,MAX_PATH, NULL, &fUsedDefaultChar); 
printf("%d %s\n",fUsedDefaultChar,myOutStr); //This prints "1 ?ello" on my system


The MSDN documentation is very clear about when lpUsedDefaultChar is set to TRUE:

lpDefaultChar [in] Optional. Pointer to the character to use if a character cannot be represented in the specified code page. The application sets this parameter to NULL if the function is to use a system default value. To obtain the system default character, the application can call the GetCPInfo or GetCPInfoEx function.

lpUsedDefaultChar [out] Optional. Pointer to a flag that indicates if the function has used a default character in the conversion. The flag is set to TRUE if one or more characters in the source string cannot be represented in the specified code page. Otherwise, the flag is set to FALSE. This parameter can be set to NULL.

That does not leave much room for misunderstanding, in my opinion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜