[C语言]unicode与utf-8编码转换(一)

Unicode与UTF-8编码转换(一)

Unicode是一个符号集合,规定了符号的二进制代码,而UTF-8是Unicode的一种实现,具体Unicode和UTF-8的联系如下所示:

        Unicode符号范围                    UTF-8编码规则
 1 | 0000 0000 - 0000 007F |                                              0xxxxxxx  
 2 | 0000 0080 - 0000 07FF |                                     110xxxxx 10xxxxxx  
 3 | 0000 0800 - 0000 FFFF |                            1110xxxx 10xxxxxx 10xxxxxx  
 4 | 0001 0000 - 0010 FFFF |                   11110xxx 10xxxxxx 10xxxxxx 10xxxxxx  
 5 | 0020 0000 - 03FF FFFF |          111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx  
 6 | 0400 0000 - 7FFF FFFF | 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx

由图可清晰的知道Unicode和UTF-8间的联系。其中UTF-8编码规则中的x就是让你编码的地方。接下来逐一举例说明各段的编码:

1,范围0x00-0x7F:给定的用例Unicode码为0x41,对应的二进制位:0100 0001,而UTF-8编码规则为:0xxxxxxx。故有:

0xxx  xxxx

+    100 0001



0100 0001

所以Unicode编码0x41转换为UTF-8后为:0x41。

所以对于区间段0x00-0x7F之间的Unicode和UTF-8编码是一致的。即与ASCII码一致(ASCII共规定了128个字符的编码)

2,范围0x80-0x7FF:给定的用例Unicode码为0x123,对应的二进制为:0001 0010 0011,而UTF-8编码规则为:110xxxxx 10xxxxxx。故有:

110x xxxx 10xx  xxxx

+       0 0100     10 0011



1100 0100 1010 0011

所以Unicode编码0x123转换为UTF-8后为:0xC4A3

3,范围0x800-0xFFFF:给定的用例Unicode码为0x4E25,对应的二进制为:0100 1110 0010 0101,而UTF-8编码规则为:1110xxxx 10xxxxxx 10xxxxxx,故有:

1110 xxxx 10xx  xxxx 10xx xxxx

+          0100     11 1000    10 0101



1110 0100 1011 1000 1010 0101

所以Unicode编码为0x4E25转换为UTF-8后为:0xE4B8A5

4,范围0x10000-0x10FFFF:给定的Unicode码为0x23456,对应的二进制为:0010 0011 0100 0101 0110,而UTF-8编码规则为:111100xx 10xxxxxx 10xxxxxx 10xxxxxx。故有:

1111 00xx 10xx xxxx  10xx xxxx 10xx xxxx

+           00     10  0011     01 0001    01 0110



1111 0000 1010 0011 1001 0001 1001 0110

所以Unicode编码为0x23456转换UTF-8后为:0xF0A39196

5,范围0x200000-0x3FFFFFF:给定的Unicode码为0x234567,对应的二进制为:0010 0011 0100 0101 0110 0111,UTF-8编码规则为:111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx。故有:

1111 10xx 10xx  xxxx 10xx xxxx  10xx xxxx 10xx xxxx

+                       00 1000    11 0100     01 0101    10 0111



1111 1000 1000 1000 1011 0100 1001 0101 1010 0111

所以Unicode编码为0x234567转换UTF-8后为:0xF888B495A7

6,范围0x4000000-0x7FFFFFFF:给定的Unicode码为0x34561234,对应的二进制为:0011 0100 0101 0110 0001 0010 0011 0100,UTF-8编码规则为:1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx。故有:

1111 110x 10xx  xxxx 10xx  xxxx 10xx xxxx  10xx xxxx  10xx xxxx

+                0    11  0100    01  0101    10 0001     00 1000     11 0100



1111 1100 1011 0100 1001 0101 1010 0001 1000 1000 1011 0100

所以Unicode编码为0x34561234转换UTF-8后为:0xFCB495A188B4

1,通过以上案例分析可得如下单字符Unicode编码转UTF-8程序为:

1)由于本系统采用大头方式(Big endian),所以先打出来的是高位的值。

2)实现思路:移动指定的位数是该字节处于易于操作的位置或使操作完的值达到指定位置,使用与运算取得指定位上的值,使用或运算实现相加效果。

int
unicode_to_utf( unsigned long unicode, unsigned char *utf )
{
    assert( utf );

    int size = 0;
    if ( unicode <= 0x7F )
    {
        *( utf + size++ ) = unicode & 0x7F;
    }
    else if ( unicode >= 0x80 && unicode <= 0x7FF )
    {
        *( utf + size++ ) = ( ( unicode >> 6 ) & 0x1F ) | 0xC0;
        *( utf + size++ ) = ( unicode & 0x3F ) | 0x80;
    }
    else if ( unicode >= 0x800 && unicode <= 0xFFFF )
    {
        *( utf + size++ ) = ( ( unicode >> 12 ) & 0x0F ) | 0xE0;
        *( utf + size++ ) = ( ( unicode >> 6  ) & 0x3F ) | 0x80;
        *( utf + size++ ) = ( unicode & 0x3F  ) | 0x80;
    }
    else if ( unicode >= 0x10000 && unicode <= 0x10FFFF )
    {
        *( utf + size++ ) = ( (unicode >> 18 ) & 0x7  ) | 0xF0;
        *( utf + size++ ) = ( (unicode >> 12 ) & 0x3F ) | 0x80;
        *( utf + size++ ) = ( (unicode >> 6  ) & 0x3F ) | 0x80;
        *( utf + size++ ) = ( unicode & 0x3F ) | 0x80;
    }
    else if ( unicode >= 0x200000 && unicode <= 0x3FFFFFF )
    {
        *( utf + size++ ) = ( (unicode >> 24 ) & 0x3  ) | 0xF8;
        *( utf + size++ ) = ( (unicode >> 18 ) & 0x3F ) | 0x80;
        *( utf + size++ ) = ( (unicode >> 12 ) & 0x3F ) | 0x80;
        *( utf + size++ ) = ( (unicode >> 6  ) & 0x3F ) | 0x80;
        *( utf + size++ ) = ( unicode & 0x3F ) | 0x80;
    }
    else if ( unicode >= 0x4000000 && unicode <= 0x7FFFFFFF )
    {
        *( utf + size++ ) = ( (unicode >> 30 ) & 0x1  ) | 0xFC;
        *( utf + size++ ) = ( (unicode >> 24 ) & 0x3F ) | 0x80;
        *( utf + size++ ) = ( (unicode >> 18 ) & 0x3F ) | 0x80;
        *( utf + size++ ) = ( (unicode >> 12 ) & 0x3F ) | 0x80;
        *( utf + size++ ) = ( (unicode >> 6  ) & 0x3F ) | 0x80;
        *( utf + size++ ) = ( unicode & 0x3F ) | 0x80;
    }
    else
    {
        printf( "Error : unknow scope\n" );
        return -1;
    }
    *( utf + size ) = ‘\0‘;

    return size;
}

测试用例如下:

int
main( int argc, char *argv[] )
{
    unsigned long unicode1 = 0x55;
    unsigned char utf[7] = { 0 };
    int size = unicode_to_utf( unicode1, utf );
    utf_print( utf, size );

    unsigned long unicode2 = 0x123;
    size = unicode_to_utf( unicode2, utf );
    utf_print( utf, size );

    memset( utf, 0x00, sizeof( utf ) );
    unsigned long unicode3 = 0x4E25;
    size = unicode_to_utf( unicode3, utf );
    utf_print( utf, size );

    memset( utf, 0x00, sizeof( utf ) );
    unsigned long unicode4 = 0x23456;
    size = unicode_to_utf( unicode4, utf );
    utf_print( utf, size );

    memset( utf, 0x00, sizeof( utf ) );
    unsigned long unicode5 = 0x234567;
    size = unicode_to_utf( unicode5, utf );
    utf_print( utf, size );

    memset( utf, 0x00, sizeof( utf ) );
    unsigned long unicode6 = 0x34561234;
    size = unicode_to_utf( unicode6, utf );
    utf_print( utf, size );

    /* error */
    memset( utf, 0x00, sizeof( utf ) );
    unsigned long unicode7 = 0x8FFFFFFF;
    size = unicode_to_utf( unicode7, utf );
    utf_print( utf, size );

    return 0;
}

打印函数如下:

void
utf_print( unsigned char *utf, int size )
{
    if ( size == -1 )
    {
        printf( "unknow scope\n" );
        return;
    }

    int index = 0;
    for ( ; index < size; index += 1 )
    {
        printf( "%X",  *( utf + index) );
    }

    printf( "\n" );
}

2,单字符UTF-8编码转Unicode编码:

int
utf_to_unicode( unsigned long utf, unsigned char *unicode )
{
    int size = 0;
    if ( utf <= 0x7F )
    {
        *( unicode + size++ ) = utf & 0x7F;
    }
    else if ( utf >= 0xC080 && utf <= 0xCFBF )
    {
        *( unicode + size++ ) = ( ( utf >> 10 ) & 0x07 );
        *( unicode + size++ ) = ( utf & 0x3F ) | ( ( ( utf >> 8 ) & 0x03 ) << 6);
    }
    else if ( utf >= 0xE08080 && utf <= 0xEFBFBF )
    {
        *( unicode + size++ ) = ( ( utf >> 10 ) & 0x0F ) | (( utf >> 16 ) & 0x0F ) << 4;
        *( unicode + size++ ) = ( utf & 0x3F ) | ((( utf >> 8 ) & 0x03 ) << 6 );
    }
    else if ( utf >= 0xF0808080 && utf <= 0xF7BFBFBF )
    {
        *( unicode + size++ ) = ( (utf >> 20 ) & 0x03 ) | ((( utf >> 24 ) & 0x07 ) << 2 );
        *( unicode + size++ ) = (( utf >> 10 ) & 0x0F ) | ( ( ( utf >> 16 ) & 0x0F ) << 4 );
        *( unicode + size++ ) = ( utf & 0x3F ) | ( ( utf >> 8 ) & 0x03 ) << 6;
        }
    else if ( utf >= 0xF880808080 && utf <= 0xFBBFBFBFBF )
    {
        *( unicode + size++ ) = ( utf >> 32 ) & 0x03;
        *( unicode + size++ ) = ( ( utf >> 20 ) & 0x03 ) | ( ( ( utf >> 24 ) & 0x3F ) << 2 );
        *( unicode + size++ ) = ( ( utf >> 10 ) & 0x0F ) | ( ( ( utf >> 16 ) & 0x0F ) << 4 );
        *( unicode + size++ ) = ( utf & 0x3F ) | ( ( (utf >> 8) & 0x03 ) << 6 );
    }
    else if ( utf >= 0xFC8080808080 && utf <= 0xFDBFBFBFBFBF )
    {
        *( unicode + size++ ) = ( ( utf >> 32 ) & 0x3F ) | ( ( ( utf >> 40 ) & 0x01 ) << 6 );
        *( unicode + size++ ) = ( ( utf >> 20 ) & 0x03 ) | ( ( ( utf >> 24 ) & 0x3F ) << 2 );
        *( unicode + size++ ) = ( ( utf >> 10 ) & 0x0F ) | ( ( ( utf >> 16 ) & 0x0F ) << 4 );
        *( unicode + size++ ) = ( utf & 0x3F ) | ( ( ( utf >> 8 ) & 0x03 ) << 6 );
    }
    else
    {
        printf( "Error : unknow scope\n" );
        return -1;
    }

    *( unicode + size ) = ‘\0‘;
    return size;
}

测试用例:

int
main( int argc, char *argv[] )
{
    unsigned char unicode[9];
    int size = 0;

    memset( unicode, 0x00, sizeof( unicode ) );
    unsigned long utf1 = 0x55;
    size = utf_to_unicode( utf1, unicode );
    unicode_print( unicode, size );

    memset( unicode, 0x00, sizeof( unicode ) );
    unsigned long utf2 = 0xC480;
    size = utf_to_unicode( utf2, unicode );
    unicode_print( unicode, size );

    memset( unicode, 0x00, sizeof( unicode ) );
    unsigned long utf3 = 0xE4B8A5;
    size = utf_to_unicode( utf3, unicode );
    unicode_print( unicode, size);

    memset( unicode, 0x00, sizeof( unicode ) );
    unsigned long utf4 = 0xF0A39196;
    size = utf_to_unicode( utf4, unicode );
    unicode_print( unicode, size);

    memset( unicode, 0x00, sizeof( unicode ) );
    unsigned long utf5 = 0xF888B495A7;
    size = utf_to_unicode( utf5, unicode );
    unicode_print( unicode, size);

    memset( unicode, 0x00, sizeof( unicode ) );
    unsigned long utf6 = 0xFCB495A188B4;
    size = utf_to_unicode( utf6, unicode );
    unicode_print( unicode, size);

    memset( unicode, 0x00, sizeof( unicode ) );
    unsigned long utf7 = 0xFEBFBFBFBFBF;
    size = utf_to_unicode( utf7, unicode );
    unicode_print( unicode, size);

    return 0;
}

打印函数如下:

void
unicode_print( unsigned char *unicode, int size )
{
    if ( size == -1 )
    {
        printf("Error : unknow scope\n");
        return;
    }

    int index = 0;
    for ( ; index < size; index += 1 )
    {
        printf( "%02X", *( unicode + index ) );
    }

    printf("\n");
}

本文参考文献:http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html

时间: 2024-08-08 09:41:39

[C语言]unicode与utf-8编码转换(一)的相关文章

ascii、unicode、utf、gb等编码详解

很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们看到8个开关状态是好的,于是他们把这称为"字节".再后来,他们又做了一些可以处理这些字节的机器,机器开动了,可以用字节来组合出很多状态,状态开始变来变去.他们看到这样是好的,于是它们就这机器称为"计算机". 开始计算机只在美国用.八位的字节一共可以组合出256(2的8次方)种不同的状态. 他们把其中的编号从0开始的32种状态分别规定了特殊的用途,一但终端.打印机遇上约定

[C语言]Unicode编码(二)-中文字符刷选

Unicode编码(二)-中文字符刷选 1,UTF-8编码中三字节中文字符的刷选方法如下: int chinese_filter(unsigned char one, unsigned char two ) {     int flag = -1;     if ( one >= 0x4E && one < 0x9F )     {         flag = 1;     }     else if ( one == 0x9F && two <= 0x

Java用native2ascii命令做unicode编码转换

背景:在做Java开发的时候,常常会出现一些乱码,或者无法正确识别或读取的文件,比如常见的validator验证用的消息资源(properties)文件就需要进行Unicode重新编码.原因是java默认的编码方式为Unicode,而我们的计算机系统编码常常是GBK等编码.需要将系统的编码转换为java正确识别的编码问题就解决了. 1.native2ascii简介:native2ascii是sun java sdk提供的一个工具.用来将别的文本类文件(比如*.txt,*.ini,*.proper

C#中文和UNICODE编码转换

C#中文和UNICODE编码转换 //中文轉為UNICODE string str = "中文"; string outStr = ""; if (!string.IsNullOrEmpty(str)) { for (int i = 0; i < str.Length; i++) { //將中文轉為10進制整數,然後轉為16進制unicode outStr += "\\u" + ((int)str[i]).ToString("x&

JavaScript编码转换之gb2312转unicode -- &amp;#X形式

http://www.cnblogs.com/meil/archive/2007/01/31/635936.html JavaScript编码转换之gb2312转unicode 1. <html><head><title>gb2312 unicode转换工具</title><div align=center><center><table border=0 cellpadding=0 cellspacing=0 style=&qu

转换编码,将Unicode编码转换成可以浏览的utf-8编码

//转换编码,将Unicode编码转换成可以浏览的utf-8编码 public function unicodeDecode($name) { $pattern = '/([\w]+)|(\\\u([\w]{4}))/i'; preg_match_all($pattern, $name, $matches); if (!empty($matches)) { $name = ''; for ($j = 0; $j < count($matches[0]); $j++) { $str = $matc

(转载)GBK、UTF8、UNICODE编码转换

GBK.UTF8.UNICODE编码转换 1 string GBKToUTF8(const std::string& strGBK) 2 { 3 int nLen = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0); 4 WCHAR * wszUTF8 = new WCHAR[nLen]; 5 MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, wszUTF8, nLen);

Python3的unicode编码转换成中文问题

Python3的unicode编码转换成中文问题 从别的地方搬过来的,担心以后不容易搜索到,就收集过来. 我当时面临的问题是要从C++发json代码出来,用python写了个server,然后返回给C++程序,结果收到的是:httpSvrDataCbUser: {"tranNO": "0808ad498670dc996", "data": "\u65b0A1EY16", "ver": "1.0&q

ASCII、GBK、Unicode和UTF-8字符编码的区别联系

很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们看到8个开关状态是好的,于是他们把这称为”字节“.再后来,他们又做了一些可以处理这些字节的机器,机器开动了,可以用字节来组合出很多状态,状态开始变来变去.他们看到这样是好的,于是它们就这机器称为”计算机“. 开始计算机只在美国用.八位的字节一共可以组合出256(2的8次方)种不同的状态. 他们把其中的编号从0开始的32种状态分别规定了特殊的用途,一但终端.打印机遇上约定好的这些字节被传过来时,就要做