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

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。