描述:
我用mbstowcs试了一下,不行,因为一个汉字由两个char构成,mbstowcs把一个汉字转化为两个unicode字符(4个字节),ft!大虾帮忙呀,最好给个例程
解决方案1:
用ATL字符转换宏的时候,有一点要小心.
以A2W为例说明:
A2W宏在转换ANSI到UNICODE时,为了加快转换的效率它是用alloca分配内存的,也就是说它是在当前的栈中分配内存,而不是在堆中.
栈空间是极其有限的,所以如果象如下使用A2W,那就很危险了.
USES_CONVERSIONT;
wchar_t* unicodeStr=L"Hello World";
while(1){
char* temp=A2W(unicodeStr );
if( ... )
break;
}
因为你的while循环是不可确定的,所以当循环次数相当大时,A2W所用的栈空间肯定会耗尽.
那时就会抱错: Stack Overflow!
我以前问过类似的,你看看是否有帮助:
http://expert.csdn.net/Expert/topic/2856/2856730.xml?temp=.5974848
http://expert.csdn.net/Expert/topic/2856/2856122.xml?temp=.1128046
这个API,很好用
The MultiByteToWideChar function maps a character string to a wide-character (Unicode) string. The character string mapped by this function is not necessarily from a multibyte character set.
int MultiByteToWideChar(
UINT CodePage, // code page
DWORD dwFlags, // character-type options
LPCSTR lpMultiByteStr, // address of string to map
int cchMultiByte, // number of bytes in string
LPWSTR lpWideCharStr, // address of wide-character buffer
int cchWideChar // size of buffer
);
ATL中有很多关于ANSI,UNICODE,DBCS的字符转换宏:
如:A2W -- ANSI到UNICODE
W2A -- UNICODE到ANSI
例子
//ConversionMacro
#include <atlbase.h>
#include <iostream.h>
#define _UNICODE
int main(void){
USES_CONVERSION;
BSTR bstr=A2BSTR(LPCSTR("Hello World 世界"));
cout << W2CA(bstr) << endl;
return 0;
}
一个汉字的两个字节的 Ascii 码都大于 128,
而一般的英文字符等的 Ascii 码小于 128,
根据这个规律你自己写一个算法转换就可以了.