多字节字符串与宽字符串之间的转换

1. Maps a character string to a UTF-16 (wide character) string. 

    Syntax:

int MultiByteToWideChar(
  UINT                              CodePage,
  DWORD                             dwFlags,
  _In_NLS_string_(cbMultiByte)LPCCH lpMultiByteStr,
  int                               cbMultiByte,
  LPWSTR                            lpWideCharStr,
  int                               cchWideChar
);
  • CodePage  
    Code page to use in performing the conversion. ( CP_ACP:The system default Windows ANSI code page.)
  • dwFlags
    Flags indicating the conversion type. ( with MB_PRECOMPOSED being the default. )
  • lpMultiByteStr
    Pointer to the character string to convert.
     
  • cbMultiByte  
    Size, in bytes, of the string indicated by the lpMultiByteStr parameter.
    Alternatively, this parameter can be set to -1 if the string is null-terminated. if cbMultiByte is 0, the function fails.

    If this parameter is -1, the function processes the entire input string, including the terminating null character. Therefore, the resulting Unicode string has a terminating null character, and the length returned by the function includes this character.

    If this parameter is set to a positive integer, the function processes exactly the specified number of bytes. If the provided size does not include a terminating null character, the resulting Unicode string is not null-terminated, and the returned length does not include this character.
  • lpWideCharStr
    Pointer to a buffer that receives the converted string.
  • cchWideChar
    Size, in characters, of the buffer indicated by lpWideCharStr.
    If this value is 0, the function returns the required buffer size, in characters, including any terminating null character, and makes no use of the lpWideCharStr buffer.

Return Value

  • Returns the number of characters written to the buffer indicated by lpWideCharStr if successful. 
  • If the function succeeds and cchWideChar is 0, the return value is the required size, in characters, for the buffer
    indicated by lpWideCharStr
  • The function returns 0 if it does not succeed. To get extended error information, the application can call GetLastError,   which can return one of the following error codes:
     ERROR_INSUFFICIENT_BUFFER. A supplied buffer size was not large enough, or it was incorrectly set to NULL.
     ERROR_INVALID_FLAGS. The values supplied for flags were not valid.
     ERROR_INVALID_PARAMETER. Any of the parameter values was invalid.
     ERROR_NO_UNICODE_TRANSLATION. Invalid Unicode was found in a string.

Example1:  把ASCII字符串转换为Unicode字符串   
 

wchar_t wszStr[100];
char szStr[100]="Hellow Windows!";
MultiByteToWideChar(CP_ACP, 0, szStr, -1, wszStr, _countof(wszStr));

Example2:查询把ASCII字符串转换为Unicode字符串所需的宽字符缓冲区的长度 
 

wchar_t wszStr[100];
char szStr[100]="Hellow Windows!";
int nStrLen = strlen(szStr);
// nStrLen = 0x0F
int nBufLen = sizeof(szStr);
// nBufLen = 0x64;
int nLenNeeded1 = MultiByteToWideChar(CP_ACP, 0, szStr, -1, NULL, 0);
// nLenNeeded1 = 0x10
int nLenNeeded2 = MultiByteToWideChar(CP_ACP, 0, szStr, strlen(szStr), NULL, 0);
// nLenNeeded2 = 0x0F
int nLenNeeded3 = MultiByteToWideChar(CP_ACP, 0, szStr, strlen(szStr)+1, NULL, 0);
// nLenNeeded3 = 0x10
int nLenNeeded4 = MultiByteToWideChar(CP_ACP, 0, szStr, sizeof(szStr), NULL, 0);
// nLenNeeded4 = 65

2. Maps a UTF-16 (wide character) string to a new character string. 

    Syntax
 

int WideCharToMultiByte(
  UINT                               CodePage,
  DWORD                              dwFlags,
  _In_NLS_string_(cchWideChar)LPCWCH lpWideCharStr,
  int                                cchWideChar,
  LPSTR                              lpMultiByteStr,
  int                                cbMultiByte,
  LPCCH                              lpDefaultChar,
  LPBOOL                             lpUsedDefaultChar
);
  • CodePage
    Code page to use in performing the conversion.
    This parameter can be set to the value of any code page that is installed or available in the operating system. 
    CP_ACP:The system default Windows ANSI code page.
     
  • dwFlags
    Flags indicating the conversion type. 
     
  • lpWideCharStr
    Pointer to the Unicode string to convert.
     
  • cchWideChar
    Size, in characters, of the string indicated by lpWideCharStr
    Alternatively, this parameter can be set to -1 if the string is null-terminated. 
    If this parameter is -1, the function processes the entire input string, including the terminating null
    character. Therefore, the resulting character string has a terminating null character, and the length
    returned by the function includes this character.
    If this parameter is set to a positive integer, the function processes exactly the specified number of
    characters. If the provided size does not include a terminating null character, the resulting character
    string is not null-terminated, and the returned length does not include this character.
  • lpMultiByteStr
    Pointer to a buffer that receives the converted string.
  • cbMultiByte
    Size, in bytes, of the buffer indicated by lpMultiByteStr.
    If this parameter is set to 0, the function returns the required buffer size for lpMultiByteStr and makes no use of the output parameter itself.
  • lpDefaultChar
    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.
  • lpUsedDefaultChar
    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.

Example1: 把Unicdoe字符串转换成ASCII字符串
 

wchar_t wszStr[100] = L"Hellow Windows!";
char szStr[100];
WideCharToMultiByte(CP_ACP, 0, wszStr, -1, szStr, sizeof(szStr), NULL, NULL);

Example2: 查询把Unicdoe字符串转换成ASCII字符串所需要的缓冲区长度

wchar_t wszStr[100] = L"Hellow Windows!";
char szStr[100];

int nWideCharStrLen = wcslen(wszStr);
// nWideCharStrLen = 0x0F
int nCapacity = _countof(wszStr);
// nCapacity = 0x64
int nBufLen = sizeof(wszStr);
// nBufLen = 0xC8
	
int nLenNeeded1 = WideCharToMultiByte(CP_ACP, 0, wszStr, -1, NULL, 0, NULL, NULL);
// nLenNeeded1 = 0x10 
int nLenNeeded2 = WideCharToMultiByte(CP_ACP, 0, wszStr, wcslen(wszStr), NULL, 0, NULL, NULL);
// nLenNeeded2 = 0x0F
int nLenNeeded3 = WideCharToMultiByte(CP_ACP, 0, wszStr, wcslen(wszStr) + 1, NULL, 0, NULL, NULL);
// nLenNeeded3 = 0x10

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值