UNICODE字符串如何转换成字符串 现有UNICODE字符串,“4F60002F7B54002F5BF9002F4E86”要求写一个函数,返回这个UNICODE字符串对应的ANSI字符~~~谁先写出这个函数,并且告诉我这些字串是什么意思就把分给他。显然,这时个7个字符的字符串。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 int UnicodeToAnsi(const unsigned char *szSrc, char *szDest, int iSrcLen){ int nDstLength; // UNICODE宽字符数目 WCHAR szTempCont[160] = {'\0'}; // UNICODE串缓冲区 // 高低字节对调,拼成UNICODE for (int i = 0; i < iSrcLen / 2; i++) { szTempCont[i] = *szSrc++ << 8; szTempCont[i] |= *szSrc++; } nDstLength = ::WideCharToMultiByte(CP_ACP, 0, szTempCont, iSrcLen / 2, szDest, 160, NULL, NULL ); szDest[nDstLength] = '\0'; return nDstLength;} 来晚了,不知道还有没有分了...WideCharToMultiByte() 你/答/对/了我是来赚分的。VBS写的。Function Uni2Asc(strUni) strHex = "0123456789ABCDEF" iLen = Len(strUni) For i = 1 To iLen Step 4 iValue = 0 strTemp = Mid(strUni , i , 4) For j = 1 To 4 chTmp = Mid(strTemp , j , 1) iTmp = InStr(strHex , chTmp) - 1 iValue = iValue*16+iTmp next strAsc = strAsc + chrw(iValue) nextEnd FunctionMsgBox(Uni2Asc("4F60002F7B54002F5BF9002F4E86")) 按照1楼的做~~nDstLength=14,结果也是乱码,明显不对 char ustring[] = "4F60002F7B54002F5BF9002F4E86"; wchar_t* wstring = new wchar_t[sizeof(ustring)/4+1]; wstring[sizeof(ustring)/4] = L'\0'; for (int i = 0, j = 0; i < sizeof(ustring)/4;++i) { int a; char temp[4] = {0}; strncpy(temp, ustring+j, 4); sscanf(temp, "%x", &a); *(wstring+i) = a; j += 4; } int nLens = ::WideCharToMultiByte(CP_ACP, 0, wstring, -1, NULL, 0, 0, 0); char* astring = new char[nLens+1]; astring[nLens] = '\0'; ::WideCharToMultiByte(CP_ACP, 0, wstring, -1, astring, nLens, 0, 0); 注意下new的部分,不用时delete,然后赋指针空. wchar_t const szBuffer[] = { L"4F60002F7B54002F5BF9002F4E86" };DWORD dwCount;char* szTarget;dwCount = ::WideCharToMultiByte(CP_ACP, NULL, szBuffer, -1, NULL, NULL, NULL, FALSE);szTarget = new char[dwCount+1];::WideCharToMultiByte(CP_ACP, NULL, szBuffer, -1, szTarget, dwCount, NULL, FALSE); //Unicode to ANSIBOOL UnicodeToANSI( char* des,const TCHAR *src ){ if(NULL == des) return FALSE; char* pElementText; int iTextLen; // wide char to multi char iTextLen = WideCharToMultiByte( CP_ACP, 0, src, -1, NULL, 0, NULL, NULL ); pElementText = new char[iTextLen + 1]; memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) ); ::WideCharToMultiByte( CP_ACP, 0, src, -1, pElementText, iTextLen, NULL, NULL ); strcpy(des,pElementText); delete[] pElementText; return TRUE;} /* 如果你不再使用这两个值,就注释掉这部分 delete [] wstring; wstring = NULL; delete [] astring; astring = NULL; */ #include "stdafx.h"#include "windows.h"#include "tchar.h"#include <iostream>using namespace std;char* ChW2A (const wchar_t* su){ char* saRet = NULL; if(su) { int nStrUnicodeLen = wcslen(su); saRet = new char[nStrUnicodeLen*2+1]; if(saRet) { memset(saRet, 0, (nStrUnicodeLen*2+1)*sizeof(char)); WideCharToMultiByte(CP_ACP, 0, su, nStrUnicodeLen, saRet, nStrUnicodeLen*2+1, NULL, NULL); } } return saRet;};int main(int argc, char* argv[]){ wchar_t w[] = {0x4F60,0x002F,0x7B54,0x002F,0x5BF9,0x002F,0x4E86,0x00}; cout << ChW2A(w) <<endl; return 0;} GetModuleHandleEx未定义 杯具被拒3次 对话框中的内存泄漏 关于游戏的网络通信问题(实在不知道发到那个区) CString类型的数据如何转为int??谢谢!! 如何向一个文本文件写入,读出内容?在线等! 在哪儿能搞到Win2000的DEBUG用的dbg文件啊? CTreeCtrl中的双击问题 使用vtoolsd出错提示:fatal error U1081: 'c:\program' : program not found 播放器实现方法 我的vc助手出问题了,不知道怎么回事???? 关于位图资源ID的合法性检测?
{
int nDstLength; // UNICODE宽字符数目
WCHAR szTempCont[160] = {'\0'}; // UNICODE串缓冲区 // 高低字节对调,拼成UNICODE
for (int i = 0; i < iSrcLen / 2; i++)
{
szTempCont[i] = *szSrc++ << 8;
szTempCont[i] |= *szSrc++;
}
nDstLength = ::WideCharToMultiByte(CP_ACP,
0,
szTempCont,
iSrcLen / 2,
szDest, 160, NULL, NULL );
szDest[nDstLength] = '\0';
return nDstLength;
}
WideCharToMultiByte()
strHex = "0123456789ABCDEF"
iLen = Len(strUni)
For i = 1 To iLen Step 4
iValue = 0
strTemp = Mid(strUni , i , 4)
For j = 1 To 4
chTmp = Mid(strTemp , j , 1)
iTmp = InStr(strHex , chTmp) - 1
iValue = iValue*16+iTmp
next
strAsc = strAsc + chrw(iValue)
next
End Function
MsgBox(Uni2Asc("4F60002F7B54002F5BF9002F4E86"))
nDstLength=14,结果也是乱码,明显不对
wchar_t* wstring = new wchar_t[sizeof(ustring)/4+1];
wstring[sizeof(ustring)/4] = L'\0';
for (int i = 0, j = 0; i < sizeof(ustring)/4;++i)
{
int a;
char temp[4] = {0};
strncpy(temp, ustring+j, 4);
sscanf(temp, "%x", &a);
*(wstring+i) = a; j += 4;
}
int nLens = ::WideCharToMultiByte(CP_ACP, 0, wstring, -1, NULL, 0, 0, 0);
char* astring = new char[nLens+1];
astring[nLens] = '\0';
::WideCharToMultiByte(CP_ACP, 0, wstring, -1, astring, nLens, 0, 0);
wchar_t const szBuffer[] = { L"4F60002F7B54002F5BF9002F4E86" };DWORD dwCount;
char* szTarget;dwCount = ::WideCharToMultiByte(CP_ACP, NULL, szBuffer, -1, NULL, NULL, NULL, FALSE);
szTarget = new char[dwCount+1];::WideCharToMultiByte(CP_ACP, NULL, szBuffer, -1, szTarget, dwCount, NULL, FALSE);
BOOL UnicodeToANSI( char* des,const TCHAR *src )
{
if(NULL == des)
return FALSE;
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_ACP,
0,
src,
-1,
NULL,
0,
NULL,
NULL );
pElementText = new char[iTextLen + 1];
memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
::WideCharToMultiByte( CP_ACP,
0,
src,
-1,
pElementText,
iTextLen,
NULL,
NULL );
strcpy(des,pElementText);
delete[] pElementText; return TRUE;
}
delete [] wstring;
wstring = NULL;
delete [] astring;
astring = NULL;
*/
#include "windows.h"
#include "tchar.h"
#include <iostream>
using namespace std;char* ChW2A (const wchar_t* su)
{
char* saRet = NULL;
if(su)
{
int nStrUnicodeLen = wcslen(su);
saRet = new char[nStrUnicodeLen*2+1];
if(saRet)
{
memset(saRet, 0, (nStrUnicodeLen*2+1)*sizeof(char));
WideCharToMultiByte(CP_ACP, 0, su, nStrUnicodeLen, saRet, nStrUnicodeLen*2+1, NULL, NULL);
}
}
return saRet;
};int main(int argc, char* argv[])
{
wchar_t w[] = {0x4F60,0x002F,0x7B54,0x002F,0x5BF9,0x002F,0x4E86,0x00};
cout << ChW2A(w) <<endl;
return 0;
}