w_char*和char *转换宽窄字符
w_char*和char*在windows编程过程中进行转换是经常需要的,通常由互联网我们取到都是utf-8编码,到windows应用程序里面却需要用unicode编码。
一开始用stdlib.h 下wcstombs_s和mbstowcs_s的代码实现,发现总是转换失败和出错。
char 转 WCHAR 、wchar_t、LPWSTR ,窄字符转宽字符,C++代码
//+------------------------------------------------------------------+ //| char to WCHAR 、wchar_t、LPWSTR etc | //+------------------------------------------------------------------+ static char* WStr2CStr(const wchar_t* WStr) { // 长度设置 size_t len = wcslen(WStr) + 1; size_t converted = 0; // 准备转换的对象 char *CStr; CStr=(char*)malloc(len*sizeof(char)); // 转换 wcstombs_s(&converted, CStr, len, WStr, _TRUNCATE); // 返回 return CStr; } |
WCHAR 、wchar_t、LPWSTR转char ,宽字符转窄字符,C++代码
//+------------------------------------------------------------------+ //| WCHAR 、wchar_t、LPWSTR to char | //+------------------------------------------------------------------+ static wchar_t* CStr2WStr(const char* CStr) { // 长度设置 size_t len = strlen(CStr) + 1; size_t converted = 0; // 准备转换的对象 wchar_t *WStr; WStr=(wchar_t*)malloc(len*sizeof(wchar_t)); // 转换 mbstowcs_s(&converted, WStr, len, CStr, _TRUNCATE); // 返回 return WStr; } |
后来调查了window系统API,使用MultiByteToWideChar和WideCharToMultiByte可以很好的解决问题。
以下来自微软官方示例代码,使用MultiByteToWideChar和WideCharToMultiByte需要调用两次,一次获取需要设定的缓冲长度,一次转换。效率有点低,但是符合微软的风格。
char 转 WCHAR 、wchar_t、LPWSTR ,窄字符转宽字符,C++代码
//+------------------------------------------------------------------+ //| char to WCHAR 、wchar_t、LPWSTR etc | //+------------------------------------------------------------------+ static wchar_t * CStr2WStr(const char *cStr) { // MultiByteToWideChar( CP_ACP, 0, chr, // strlen(chr)+1, wchar, size/sizeof(wchar[0]) ); // First: get count of multi-byte string. const DWORD cCh = MultiByteToWideChar(CP_ACP, // Character Page. 0, // Flag, always be 0. cStr, // Multi-byte string. -1, // '-1' is to determind length automatically. NULL, // 'NULL' means ignore result. This is based // on next argument is '0'. 0); // '0' is to get the count of character needed // instead of to translate. // Second: allocate enough memory to save result in wide character. wchar_t* wStr = new wchar_t[cCh]; ZeroMemory(wStr, cCh * sizeof(wStr[0])); // Third: Translate it! MultiByteToWideChar(CP_ACP, // Character Page. 0, // Flag, always be 0. cStr, // Multi-byte string. -1, // Count of character of string above. wStr, // Target wide character buffer. cCh); // Count of character of wide character string buffer. return wStr; } |
WCHAR 、wchar_t、LPWSTR转char ,宽字符转窄字符,C++代码
//+------------------------------------------------------------------+ //| WCHAR 、wchar_t、LPWSTR to char | //+------------------------------------------------------------------+ static char* WStr2CStr(const wchar_t *wchar) { //WideCharToMultiByte( CP_ACP, 0, wchar, -1, // chr, length, NULL, NULL ); const DWORD cCh = WideCharToMultiByte(CP_ACP, 0, wchar, -1, NULL, 0, NULL, // When the wide character to be traslated // is not in code page, proc will use this // variable to fill with. We usually set it // to NULL. NULL); // This variable will save the number of // character which can't be tranlated. We // usually set it to NULL. // Second: allocate enough memory to save result. char* cStr = new char[cCh]; ZeroMemory(cStr, cCh * sizeof(cStr[0])); // Third: Translate it! WideCharToMultiByte(CP_ACP, 0, wchar, -1, cStr, cCh, NULL, NULL); return cStr; } |
参考
http://blog.163.com/tianshi_17th/blog/static/4856418920085209414977/
http://www.cnblogs.com/zhwl/archive/2012/11/23/2784282.html
http://www.cnblogs.com/wind-net/archive/2012/10/31/2718329.html
Recent Comments