查查MSDN吧...使用W2A macro, // Convert LPCWSTR to LPCSTR. void ExampleFunction1(LPCWSTR pszW) { // Create an instance of CW2A, called pszA, // and initialize it with pszW. CW2A pszA(pszW); // pszA works like an LPCSTR, and can be used thus: ExampleFunctionA(pszA); // Note: pszA will become invalid when it goes out of scope. }
// Convert LPCWSTR to LPCSTR.
void ExampleFunction1(LPCWSTR pszW)
{
// Create an instance of CW2A, called pszA,
// and initialize it with pszW.
CW2A pszA(pszW);
// pszA works like an LPCSTR, and can be used thus:
ExampleFunctionA(pszA);
// Note: pszA will become invalid when it goes out of scope.
}
wchar_t* pwchar = L"hello";
char* pchar= new char[16];
pchar = (CW2A)pwchar;
WideCharToMultiByte(CP_ACP, 0, wszSource, -1, szTarget, 255, NULL, NULL);
{
memset(d,'\0',iLen+1);//目标缓存区长度要比实际长度至少多一个字节
if(WideCharToMultiByte(CP_ACP, 0, s,-1,d, iLen, NULL, NULL)==0)
{
return NULL;//失败
}
return d;//成功,返回d指针
}
//Unicode转换Ansi处理
LPWSTR pwbBuffer=new WCHAR[255];
PBYTE sourValue = new BYTE[255]; DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,pwbBuffer,-1,NULL,0,NULL,FALSE);//计算要使用空间
dwCount=dwNum;//unicode返回的值要是转换后的大小 if(!(sourValue = (BYTE *)malloc(dwNum)))
{
return false;
} WideCharToMultiByte (CP_OEMCP,NULL,pwbBuffer,-1,(PBYTE)sourValue,dwNum,NULL,FALSE);delete[] pwbBuffer;
delete[] sourValue ;
{
int len=MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, NULL,0);
unsigned short * wszGBK = new unsigned short[len+1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK=new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte (CP_ACP, 0, wszGBK, -1, szGBK, len, NULL,NULL);
strUtf8 = szGBK;
delete[] szGBK;
delete[] wszGBK;
}int convertUTF16UTF8(char* utf16, int& size16, char* utf8, int& size8)
{
int i=0, count=0;
unsigned short int integer;
for(i=0;i<size16;i+=2)
{
integer = *(unsigned short int*)&utf16[i];
if( integer<0x0080)
{
utf8[count] = integer & 0x7f | 0x00;
count++;
}
else if( integer>=0x0080 && integer<=0x07ff)
{
utf8[count] = 0xC0 | (0x1F & integer>>6);
utf8[count+1] = 0x80 | (0x3F & integer);
count+=2;
}
else if( integer>=0x0800 )
{
utf8[count] = 0xE0 | (0x0F & integer>>12);
utf8[count+1] = 0x80 | ((0x0FC0 & integer)>>6);
utf8[count+2] = 0x80 | (0x003F & integer);
count += 3;
}
else
{
count = 0;
return count;
}
}
size16 = i;
size8 = count;
return count;
}
bstrData = strData;
nLen = WideCharToMultiByte(CP_ACP, NULL, bstrData, bstrData.Length() , NULL, NULL, NULL, NULL);
lpszData = (char *)malloc(nLen + 1);
memset(lpszData,0,nLen + 1);
WideCharToMultiByte(CP_UTF8, NULL, bstrData, bstrData.Length(), lpszData, nLen, NULL, NULL);CString strRes = (CString)bstrData;
// unicode字符串转ansi字符串
// 返回大于0成功,小于0失败
//**************************************
int
ustr_astr( WCHAR *unicodestr, char *ansistr )
{
int result = 0;
try
{
int needlen = WideCharToMultiByte( CP_ACP, 0, unicodestr, -1, NULL, 0, NULL, NULL );
if( needlen < 0 )
{
return needlen;
} result = WideCharToMultiByte( CP_ACP, 0, unicodestr, -1, ansistr, needlen + 1, NULL, NULL );
if( result < 0 )
{
return result;
}
return strlen( ansistr );
}
catch( ... )
{
ShowError();
}
return result;
}
A2CW (LPCSTR) -> (LPCWSTR)
A2W (LPCSTR) -> (LPWSTR)
W2CA (LPCWSTR) -> (LPCSTR)
W2A (LPCWSTR) -> (LPSTR)
非常体贴!
#include <atlbase.h>
#include <atlconv.h>
然后写入如下代码:
CString text1=_T("121313");
LPTSTR txt1=text1.GetBuffer();
USES_CONVERSION;
const char* pSth1=T2A(txt1);
text1.ReleaseBuffer();
找到表格间的对应关系就好了
--------------------------------
可以hardcode因为这个都是固定的
也可以看看是否能建立映射关系
---------------------