UTF8与GB2312之间的转换Word文件下载.docx
《UTF8与GB2312之间的转换Word文件下载.docx》由会员分享,可在线阅读,更多相关《UTF8与GB2312之间的转换Word文件下载.docx(8页珍藏版)》请在冰豆网上搜索。
intcbMultiByte,//sizeofbuffer
LPCSTRlpDefaultChar,//defaultforunmappablechars
LPBOOLlpUsedDefaultChar//setwhendefaultcharused);
//将宽字符转换成多个窄字符intMultiByteToWideChar(
DWORDdwFlags,//character-typeoptions
LPCSTRlpMultiByteStr,//stringtomap
intcbMultiByte,//numberofbytesinstring
LPWSTRlpWideCharStr,//wide-characterbuffer
intcchWideChar//sizeofbuffer
);
//将多个窄字符转换成宽字符需要用到的一些函数:
CStringCXmlProcess:
:
HexToBin(CStringstring)//将16进制数转换成2进制
{
if(string=="
0"
)return"
0000"
;
1"
0001"
2"
0010"
if(string=
="
3"
0011"
4"
0100"
5"
0101"
6"
0110"
7"
0111"
8"
1000"
9"
1001"
a"
1010"
b"
1011"
c"
1100"
d"
1101"
e"
1110"
f"
1111"
return"
"
CStringCXmlProcess:
BinToHex(CStringBinString)//将2进制数转换成16进制
if(BinString=="
if(BinString=="
if(BinString=
}
进制字符数据转
intCXmlProcess:
BinToInt(CStringstring)//2换成10进制整型
intlen=0;
inttempInt=0;
infs=r-nfH0八
fo「(infiHo二Asmng.GaLengfho二++)宀
CDmp-nf丄八
s=r-nfH(inoss.ng.GefAs—48」foanfkHo八kA7—i八k++)
宀
CDmp-nfH2*CDmp-nc
-en+HCDmp-nf*s=r-nc
refum-ep
)UTFOO>
旃注GB2312出mUTFOO>
旃注Unicode•^可囲瞥Unicode镒ff因選widechaHOMU-HByCDM旃注GB2312
WCHAR*cxm-p「ocessxuTF—8Tounicode(cha「*us5rli)lmUTFOO>
旃注Unicode
charchar—one八
charchar—fwp
charcha匚hree八
infHchan
intLchar;
charuchar[2];
WCHAR*unicode;
CStringstring_one;
CStringstring_two;
CStringstring_three;
CStringcombiString;
char_one=*ustart;
char_two=*(ustart+1);
char_three=*(ustart+2);
string_one.Format("
%x"
char_one);
string_two.Format("
char_two);
string_three.Format("
char_three);
string_three=string_three.Right
(2);
string_two=string_two.Right
(2);
string_one=string_one.Right
(2);
string_three=
HexToBin(string_three.Left
(1))+HexToBin(string_three.Right
(1));
string_two=
HexToBin(string_two.Left
(1))+HexToBin(string_two.Right
(1));
string_one=
HexToBin(string_one.Left
(1))+HexToBin(string_one.Right
(1));
combiString=string_one+string_two+string_three;
combiString=combiString.Right(20);
combiString.Delete(4,2);
combiString.Delete(10,2);
Hchar=BinToInt(combiString.Left(8));
Lchar=BinToInt(combiString.Right(8));
uchar[1]=(char)Hchar;
uchar[0]=(char)Lchar;
unicode=(WCHAR*)uchar;
returnunicode;
char*CXmlProcess:
UnicodeToGB2312(unsignedshortuData)
//把Unicode转换成GB2312
char*buffer;
buffer=newchar[sizeof(WCHAR)];
WideCharToMultiByte(CP_ACP,NULL,&
uData,1,buffer,sizeof(WCHAR),NULL,NULL);
returnbuffer;
GB2312转换成UTF-8:
先把GB2312通过函数MultiByteToWideChar转换成Unicode.然后再把Unicode通过拆开Unicode后拼装成UTF-8。
WCHAR*
CXmlProcess:
Gb2312ToUnicode(char*gbBuffer)//GB2312转换成Unicode
WCHAR*uniChar;
uniChar=newWCHAR[1];
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,uniChar,1);
returnuniChar;
UnicodeToUTF_8(WCHAR*UniChar)//Unicode转换成UTF-8
char*buffer;
CStringstrOne;
CStringstrTwo;
CStringstrThree;
CStringstrFour;
CStringstrAnd;
buffer=newchar[3];
inthInt,lInt;
hInt=(int)((*UniChar)/256);
lInt=(*UniChar)%256;
CStringstring;
string.Format("
hInt);
strTwo=HexToBin(string.Right
(1));
string=string.Left(string.GetLength()-1);
strOne=HexToBin(string.Right
(1));
string.Format("
lInt);
strFour=HexToBin(string.Right
(1));
string=string.Left(string.GetLength()-1);
strThree=HexToBin(string.Right
(1));
strAnd=strOne+strTwo+strThree+strFour;
strAnd.Insert(0,"
strAnd.Insert(8,"
10"
strAnd.Insert(16,"
strOne=strAnd.Left(8);
strAnd=strAnd.Right(16);
strTwo=strAnd.Left(8);
strThree=strAnd.Right(8);
*buffer=(char)BinToInt(strOne);
buffer[1]=(char)BinToInt(strTwo);
buffer[2]=(char)BinToInt(strThree);
}例子:
将GB2312转换成UTF-8的调用:
char*CXmlProcess:
translateCharToUTF_8(char*xmlStream,intlen){
intnewCharLen=0;
intoldCharLen=0;
intrevCharLen=len;
char*newCharBuffer;
char*finalCharBuffer;
CStringstring;
newCharBuffer=newchar[int(1.5*revCharLen)];
//设置最大的一个缓冲区
while(oldCharLen=0)
*(newCharBuffer+newCharLen)=*(xmlStream
+oldCharLen);
newCharLen++;
oldCharLen++;
}//如果是英文直接复制就可以
else
WCHAR*pbuffer=this->
Gb2312ToUnicode(xmlStream+oldCharLen);
buffer=this->
UnicodeToUTF_8(pbuffer);
*(newCharBuffer+newCharLen)=*buffer;
*(newCharBuffer+newCharLen+1)=*(buffer+1);
*(newCharBuffer+newCharLen+2)=*(buffer+2);
newCharLen+=3;
oldCharLen+=2;
newCharBuffer[newCharLen]='
'
\0'
CStringstring1;
string1.Format("
%s"
newCharBuffer);
finalCharBuffer=newchar[newCharLen+1];
memcpy(finalCharBuffer,newCharBuffer,newCharLen+1);
returnfinalCharBuffer;
}程序都非常的简单,由于实在太穷。
已经吃了两天的方便面。
所以现在头昏,程序的详细说明就不写了。
程序员到了像我这样的地步也真是少见。
工资低没有办法。
哎!
!