Chinaunix首页 | 论坛 | 博客
  • 博客访问: 239079
  • 博文数量: 131
  • 博客积分: 259
  • 博客等级: 二等列兵
  • 技术积分: 705
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-21 21:15
文章分类

全部博文(131)

文章存档

2013年(3)

2011年(128)

分类: C/C++

2013-04-12 10:43:08

std::string  ChineseToUTF_8(wchar_t wch);
int GetCharTotalBytes(wchar_t wch);
BYTE GetCharByteIndex(int nstart,int nto,wchar_t wch);


int main()
{
wchar_t p=L'粤';
std::string str=ChineseToUTF_8(p);
return 0;
}


std::string  ChineseToUTF_8(wchar_t wch)
{
std::string strreturn="";
unsigned char hexchars[] = "0123456789ABCDEF";
int nbytecount=GetCharTotalBytes(wch);
if (nbytecount>=0&&nbytecount<=7)
{
BYTE byte1;
byte1=GetCharByteIndex(1,7,wch);


strreturn+="%";
strreturn+=hexchars[byte1>>4];
strreturn+=hexchars[byte1&0x0F];


}
else if (nbytecount>=8&&nbytecount<=11)
{
BYTE byte1[2];
byte1[0]=GetCharByteIndex(7,5,wch)|0xC0;
byte1[1]=GetCharByteIndex(1,6,wch)|0x80;
for (int i=0;i<2;i++)
{
strreturn+="%";
strreturn+=hexchars[byte1[i]>>4];
strreturn+=hexchars[byte1[i]&0x0F];
}
}
else if (nbytecount>=12&&nbytecount<=16)
{
BYTE byte1[3];
byte1[0]=GetCharByteIndex(13,3,wch)|0xE0;
byte1[1]=GetCharByteIndex(7,6,wch)|0x80;
byte1[2]=GetCharByteIndex(1,6,wch)|0x80;
for (int i=0;i<3;i++)
{
strreturn+="%";
strreturn+=hexchars[byte1[i]>>4];
strreturn+=hexchars[byte1[i]&0x0F];
}
}
else if (nbytecount>=17&&nbytecount<=21)
{
BYTE byte1[4];
byte1[0]=GetCharByteIndex(19,3,wch)|0xF0;
byte1[1]=GetCharByteIndex(13,6,wch)|0x80;
byte1[2]=GetCharByteIndex(7,6,wch)|0x80;
byte1[3]=GetCharByteIndex(1,6,wch)|0x80;
for (int i=0;i<4;i++)
{
strreturn+="%";
strreturn+=hexchars[byte1[i]>>4];
strreturn+=hexchars[byte1[i]&0x0F];
}
}
else if (nbytecount>=22&&nbytecount<=26)
{
BYTE byte1[5];
byte1[0]=GetCharByteIndex(25,2,wch)|0xF8;
byte1[1]=GetCharByteIndex(19,6,wch)|0x80;
byte1[2]=GetCharByteIndex(13,6,wch)|0x80;
byte1[3]=GetCharByteIndex(7,6,wch)|0x80;
byte1[4]=GetCharByteIndex(1,6,wch)|0x80;
for (int i=0;i<5;i++)
{
strreturn+="%";
strreturn+=hexchars[byte1[i]>>4];
strreturn+=hexchars[byte1[i]&0x0F];
}
}
else if (nbytecount>=27&&nbytecount<=31)
{
BYTE byte1[6];
byte1[0]=GetCharByteIndex(31,1,wch)|0xF8;
byte1[1]=GetCharByteIndex(25,6,wch)|0x80;
byte1[2]=GetCharByteIndex(19,6,wch)|0x80;
byte1[3]=GetCharByteIndex(13,6,wch)|0x80;
byte1[4]=GetCharByteIndex(7,6,wch)|0x80;
byte1[5]=GetCharByteIndex(1,6,wch)|0x80;
for (int i=0;i<6;i++)
{
strreturn+="%";
strreturn+=hexchars[byte1[i]>>4];
strreturn+=hexchars[byte1[i]&0x0F];
}
}
else
{


}


return strreturn;
}
int GetCharTotalBytes(wchar_t wch)
{
int n=0;
do
{
n++;
}while(((wch=wch>>1))>0);
return n;
}
BYTE GetCharByteIndex(int nstart,int ncount,wchar_t wch)
{
BYTE n=0;
wch=wch>>(nstart-1);
int ntotalcount=ncount;
BYTE *pthebyte=new BYTE(ncount);
int nidx=0;
while((ncount--)>0)
{
BYTE nbyte=wch&0x1;
pthebyte[nidx++]=nbyte;
wch=wch>>1;
}
for (int i=ntotalcount-1;i>=0;i--)
{
if (pthebyte[i])
{
n=(n<<1)|1;
}
else
{
n=(n<<1)|0;
}
}
return n;
}


阅读(2030) | 评论(0) | 转发(0) |
0

上一篇:链表就地逆置

下一篇:通信协议之序列化

给主人留下些什么吧!~~