9 个解决方案
#1
使用MultiByteToWideChar转换为Unicode
#2
使用 _tcscpy_s 替换 strcpy
#3
之前看到一段代码是这样处理的,给你分享一下
ANSI to Unicode
wstring ANSIToUnicode( const string& str )
{
int len = 0;
len = str.length();
int unicodeLen = ::MultiByteToWideChar( CP_ACP,
0,
str.c_str(),
-1,
NULL,
0 );
wchar_t * pUnicode;
pUnicode = new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
::MultiByteToWideChar( CP_ACP,
0,
str.c_str(),
-1,
(LPWSTR)pUnicode,
unicodeLen );
wstring rt;
rt = ( wchar_t* )pUnicode;
delete pUnicode;
return rt;
}
#4
A2W
#5
这个问题确实遇到过,也是在处理网络数据的时候遇到的。
#6
wchar_t* toUTF8(CString& str){
//char * pStr = (LPCSTR)(str.GetBuffer(str.GetLength()));; //取得str对象的原始字符串
//_CrtMemState s1, s2, s3;
//_CrtMemCheckpoint(&s1);
int nBufferSize = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)(str.GetBuffer(str.GetLength())), -1, NULL, 0); //取得所需缓存的多少
wchar_t *pBuffer = (wchar_t*)malloc(nBufferSize * sizeof(wchar_t));//申请缓存空间
//_CrtMemCheckpoint(&s2);
//if(_CrtMemDifference(&s3, &s1, &s2))
// _CrtMemDumpStatistics(&s3);
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)(str.GetBuffer(str.GetLength())), -1 , pBuffer, nBufferSize*sizeof(wchar_t));//转码
//MessageBoxW(NULL, pBuffer, L"Text", MB_OK); //显示
//free(pBuffer); //释放缓存
return pBuffer;
}
#7
你可以参考下,看是否符合你的要求
#8
#include <tchar.h>
#include <strsafe.h>
_tcscpy_s();
StringCchPrintf();
#include <strsafe.h>
_tcscpy_s();
StringCchPrintf();
#9
仅供参考:
#include <afxpriv.h>
#include <stdio.h>
char str[3]="汉";
wchar_t ws[2];
int main() {
USES_CONVERSION;
swprintf(ws,L"%s",A2W(str));
printf("%04hX\n",(unsigned short)ws[0]);
return 0;
}
//6C49
//
#1
使用MultiByteToWideChar转换为Unicode
#2
使用 _tcscpy_s 替换 strcpy
#3
之前看到一段代码是这样处理的,给你分享一下
ANSI to Unicode
wstring ANSIToUnicode( const string& str )
{
int len = 0;
len = str.length();
int unicodeLen = ::MultiByteToWideChar( CP_ACP,
0,
str.c_str(),
-1,
NULL,
0 );
wchar_t * pUnicode;
pUnicode = new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
::MultiByteToWideChar( CP_ACP,
0,
str.c_str(),
-1,
(LPWSTR)pUnicode,
unicodeLen );
wstring rt;
rt = ( wchar_t* )pUnicode;
delete pUnicode;
return rt;
}
#4
A2W
#5
这个问题确实遇到过,也是在处理网络数据的时候遇到的。
#6
wchar_t* toUTF8(CString& str){
//char * pStr = (LPCSTR)(str.GetBuffer(str.GetLength()));; //取得str对象的原始字符串
//_CrtMemState s1, s2, s3;
//_CrtMemCheckpoint(&s1);
int nBufferSize = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)(str.GetBuffer(str.GetLength())), -1, NULL, 0); //取得所需缓存的多少
wchar_t *pBuffer = (wchar_t*)malloc(nBufferSize * sizeof(wchar_t));//申请缓存空间
//_CrtMemCheckpoint(&s2);
//if(_CrtMemDifference(&s3, &s1, &s2))
// _CrtMemDumpStatistics(&s3);
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)(str.GetBuffer(str.GetLength())), -1 , pBuffer, nBufferSize*sizeof(wchar_t));//转码
//MessageBoxW(NULL, pBuffer, L"Text", MB_OK); //显示
//free(pBuffer); //释放缓存
return pBuffer;
}
#7
你可以参考下,看是否符合你的要求
#8
#include <tchar.h>
#include <strsafe.h>
_tcscpy_s();
StringCchPrintf();
#include <strsafe.h>
_tcscpy_s();
StringCchPrintf();
#9
仅供参考:
#include <afxpriv.h>
#include <stdio.h>
char str[3]="汉";
wchar_t ws[2];
int main() {
USES_CONVERSION;
swprintf(ws,L"%s",A2W(str));
printf("%04hX\n",(unsigned short)ws[0]);
return 0;
}
//6C49
//