w_char*和char *转换宽窄字符

来源:互联网 发布:mac修复win7引导 编辑:程序博客网 时间:2024/04/27 19:47
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




--------------------------------------------------------------------------------------

- 版权声明:

- 如在本页面内无特别说明,本文内容均为[李大仁博客]原创,本文版权归[李大仁博客]所有。

- 欢迎转载,转载请务必在文章页面明显位置提供原文链接并注明出处。欢迎您在转载本文时保留本段声明。

- 文章标题: w_char*和char *转换宽窄字符

- 独立博客:李大仁博客

- 永久链接:http://www.lidaren.com/archives/1660

--------------------------------------------------------------------------------------

以上内容由博客自动发布工具自动发布,最终显示内容和效果会与原文内容有所偏差,敬请谅解。




0 0
原创粉丝点击