banner
李大仁博客

李大仁博客

天地虽大,但有一念向善,心存良知,虽凡夫俗子,皆可为圣贤。

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

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.