现在我要把这个UTF8字符串保存在一个UNICODE版本的CString里面,请问如何做?
8 个解决方案
#1
http://www.vckbase.com/document/viewdoc/?id=1397
#2
MultiByteToWideChar
#3
//一个中文字符串的UTF8代码,可以猜猜看
BYTE byteUTF8[]={0xE7,0x94, 0xB7, 0xE5, 0x84, 0xBF, 0xE5, 0xBD, 0x93, 0xE8, 0x87, 0xAA, 0xE5, 0xBC, 0xBA};
BYTE result[128];
// utf-8 to uniocde ,1 get size
int nwLen = MultiByteToWideChar(CP_UTF8,0,(LPSTR)byteUTF8,-1,NULL,0);
TRACE( "UTF8 String len:%d\n ",nwLen);
LPWSTR lpw=new WCHAR[nwLen+1];
TRACE( "sizeof(lpw):%d\n ",(nwLen+1)*sizeof(WCHAR));
memset (lpw,0,(nwLen+1)*sizeof(WCHAR));
// utf-8 to uniocde ,1 convert
MultiByteToWideChar( CP_UTF8, 0, (LPSTR)byteUTF8,
sizeof(byteUTF8), (LPWSTR)lpw, nwLen );
memset(result,0,sizeof(result));
// unicode to LPSTR
WideCharToMultiByte( CP_ACP, 0,lpw, -1,
(LPSTR)result, sizeof(result), NULL, NULL );
TRACE( "ANSI String=%s\n ",result);
delete[] lpw;
#4
查以下相关的函数
#5
//utf-8转unicode
wchar_t *WebServiesGetWeather::Utf_8ToUnicode(char* szU8)
{
//UTF8 to Unicode
//由于中文直接复制过来会成乱码,编译器有时会报错,故采用16进制形式
//预转换,得到所需空间的大小
int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0);
//分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间
wchar_t* wszString = new wchar_t[wcsLen + 1];
//转换
::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), wszString, wcsLen);
//最后加上'\0'
wszString[wcsLen] = '\0';
return wszString;
}
//unicode转utf-8
char*WebServiesGetWeather::UnicodeToUTF_8First(CString str)
{
int u8Len =WideCharToMultiByte(CP_UTF8, NULL,CStringW(str),str.GetLength(), NULL, 0, NULL, NULL);
char* szU8 = new char[u8Len + 1];
WideCharToMultiByte(CP_UTF8, NULL, CStringW(str), str.GetLength(), szU8, u8Len, NULL, NULL);
szU8[u8Len] = '\0';
return szU8; }
#6
这个正解
#7
很好, 很详细
#8
MultiByteToWideChar---------windows下涉及字符转换的基本上用这个函数全部搞定了。
#1
http://www.vckbase.com/document/viewdoc/?id=1397
#2
MultiByteToWideChar
#3
//一个中文字符串的UTF8代码,可以猜猜看
BYTE byteUTF8[]={0xE7,0x94, 0xB7, 0xE5, 0x84, 0xBF, 0xE5, 0xBD, 0x93, 0xE8, 0x87, 0xAA, 0xE5, 0xBC, 0xBA};
BYTE result[128];
// utf-8 to uniocde ,1 get size
int nwLen = MultiByteToWideChar(CP_UTF8,0,(LPSTR)byteUTF8,-1,NULL,0);
TRACE( "UTF8 String len:%d\n ",nwLen);
LPWSTR lpw=new WCHAR[nwLen+1];
TRACE( "sizeof(lpw):%d\n ",(nwLen+1)*sizeof(WCHAR));
memset (lpw,0,(nwLen+1)*sizeof(WCHAR));
// utf-8 to uniocde ,1 convert
MultiByteToWideChar( CP_UTF8, 0, (LPSTR)byteUTF8,
sizeof(byteUTF8), (LPWSTR)lpw, nwLen );
memset(result,0,sizeof(result));
// unicode to LPSTR
WideCharToMultiByte( CP_ACP, 0,lpw, -1,
(LPSTR)result, sizeof(result), NULL, NULL );
TRACE( "ANSI String=%s\n ",result);
delete[] lpw;
#4
查以下相关的函数
#5
//utf-8转unicode
wchar_t *WebServiesGetWeather::Utf_8ToUnicode(char* szU8)
{
//UTF8 to Unicode
//由于中文直接复制过来会成乱码,编译器有时会报错,故采用16进制形式
//预转换,得到所需空间的大小
int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0);
//分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间
wchar_t* wszString = new wchar_t[wcsLen + 1];
//转换
::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), wszString, wcsLen);
//最后加上'\0'
wszString[wcsLen] = '\0';
return wszString;
}
//unicode转utf-8
char*WebServiesGetWeather::UnicodeToUTF_8First(CString str)
{
int u8Len =WideCharToMultiByte(CP_UTF8, NULL,CStringW(str),str.GetLength(), NULL, 0, NULL, NULL);
char* szU8 = new char[u8Len + 1];
WideCharToMultiByte(CP_UTF8, NULL, CStringW(str), str.GetLength(), szU8, u8Len, NULL, NULL);
szU8[u8Len] = '\0';
return szU8; }
#6
这个正解
#7
很好, 很详细
#8
MultiByteToWideChar---------windows下涉及字符转换的基本上用这个函数全部搞定了。