16,472
社区成员
发帖
与我相关
我的任务
分享
char *
u2s( unsigned char *buf, unsigned short length, char *tmpbuf )
{
int i;
char *p;
char s[ 16 ] = "";
wchar_t wstr[ 1 ];
CString mstr;
int l, curlen;
*tmpbuf = 0;
curlen = 0;
mstr = _T( "" );
for( i = 0; i < ( int )length; i += 2 )
{
p = ( char * )wstr;
*p = *( buf + 1 ); //high
*( p + 1 ) = *buf; //low
mstr = wstr;
memset( s, 0, sizeof( s ) );
strcat( s, ( const char * )mstr );
if( *buf ) //if low is not 0
{
s[ 2 ] = 0;
l = 2;
}
else
{
s[ 1 ] = 0;
l = 1;
}
strcat( tmpbuf + curlen, ( const char * )s );
curlen += l;
buf += 2;
}
return tmpbuf;
}
char *
utf82ascii( unsigned char *utf8, unsigned short length, char *buf )
{
int i = 0;
unsigned short a;
unsigned char b;
char *p = buf;
unsigned char t[ 2 ];
do
{
if( 0xe0 == ( *utf8 & 0xf0 ) &&
0x80 == ( *( utf8 + 1 ) & 0xc0 ) &&
0x80 == ( *( utf8 + 2 ) & 0xc0 ) )
{
//3个字节
b = *utf8;
b &= 0x0f;
a = b;
a <<= 6;
b = *( utf8 + 1 );
b &= 0x3f;
a |= b;
a <<= 6;
b = *( utf8 + 2 );
b &= 0x3f;
a |= b;
t[ 0 ] = ( a >> 8 ) & 0xff;
t[ 1 ] = ( a & 0xff );
u2s( t, 2, p );
p += 2;
i += 3;
utf8 += 3;
}
else if ( 0xc0 == ( *utf8 & 0xe0 ) &&
0x80 == ( *( utf8 + 1 ) & 0xc0 ) )
{
//2个字节
b = *utf8;
b &= 0x1f;
a = b;
a <<= 6;
b = *( utf8 + 1 );
b &= 0x3f;
a |= b;
t[ 0 ] = ( a >> 8 ) & 0xff;
t[ 1 ] = ( a & 0xff );
u2s( t, 2, p );
p += 2;
i += 2;
utf8 += 2;
}
else
{
//1个字节
*( p++ ) = *( utf8++ );
i++;
}
} while ( i < length );
*p = 0;
return buf;
}