Tchar 和 char 之间的转换

来源:互联网 发布:哪个软件可以合成照片 编辑:程序博客网 时间:2024/06/09 23:48

 

 

Unicode和ANSI之间的转换真麻烦

 

现在在搞一个传输工具,用socket的send函数可是传输的缓冲区只能是char的

 

搞的我所有的TCHAR都得向char转换,真麻烦,搜到了几个函数

 

1. MultiByteToWideChar

从名字就知道了是多字节向宽字节的转换函数

 

摘自msdn

Maps a character string to a wide character (Unicode UTF-16) string. The character string mapped by this function is not necessarily from a multibyte character set.

int MultiByteToWideChar(  UINT CodePage,   DWORD dwFlags,           LPCSTR lpMultiByteStr,   int cbMultiByte,         LPWSTR lpWideCharStr,    int cchWideChar        );
CodePage 这个参数意思没怎么看,反正我每次都取CP_ACP
dwFlags 这个参数我每次都取0
lpMultiByteStr
[in] Pointer to the character string to convert.
cbMultiByte
[in] 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. Note that, if cbMultiByte is 0, the function fails.

If this parameter is -1, the function processes the entire input string, including the null terminator. Therefore, the resulting wide character string has a null terminator, and the length returned by the function includes the terminating null 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 null terminator, the resulting wide character string is not null-terminated, and the returned length does not include the terminating null character.

lpWideCharStr
[out] Pointer to a buffer that receives the converted string.
cchWideChar
[in] Size, in WCHAR values, of the buffer indicated by lpWideCharStr. If this value is 0, the function returns the required buffer size, in WCHAR values, including any terminating null character, and makes no use of the lpWideCharStr buffer.  
2. WideCharToMultiByte 
从名字就知道了是宽字节向多字节的转换函数
摘自msdn
Maps a wide character string to a new character string. The new character string is not necessarily from a multibyte character set. 
int WideCharToMultiByte(  UINT CodePage,   DWORD dwFlags,   LPCWSTR lpWideCharStr,  int cchWideChar,   LPSTR lpMultiByteStr,   int cbMultiByte,  LPCSTR lpDefaultChar,      LPBOOL lpUsedDefaultChar);
CodePage 这个参数意思没怎么看,反正我每次都取CP_ACP
dwFlags 这个参数我每次都取0
lpWideCharStr
[in] Pointer to the wide character string to convert.
cchWideChar
[in] Size, in WCHAR values, of the string indicated by lpWideCharStr. If this parameter is set to -1, the function assumes the string to be null-terminated and calculates the length automatically, including the null terminator. If cchWideChar is set to 0, the function fails.
lpMultiByteStr
[out] Pointer to a buffer that receives the converted string.
cbMultiByte
[in] 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
[in] Pointer to the character to use if a wide character cannot be represented in the specified code page. The application sets this parameter to a null pointer 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.

For the CP_UTF7 and CP_UTF8 settings for CodePage, this parameter must be set to a null pointer. Otherwise, the function fails with ERROR_INVALID_PARAMETER.

lpUsedDefaultChar
[out] 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 a null pointer.

For the CP_UTF7 and CP_UTF8 settings for CodePage, this parameter must be set to a null pointer. Otherwise, the function fails with ERROR_INVALID_PARAMETER.

 

 

自己mark一下,免得以后难找

 

原创粉丝点击