UTF-8与UNICODE的关系及代码转换

所谓“utf-8”只是UCS Transformation Format,只是UNICODE的一种表现形式,不等同于UNICODE,一般汉字在UNICODE中为两个(双)字节表示,而我们看到实际保存的文档确是三个字节表示一个汉字的,看看下表:

U-00000000 - U-0000007F:  0xxxxxxx
U-00000080 - U-000007FF:  110xxxxx 10xxxxxx
U-00000800 - U-0000FFFF:  1110xxxx 10xxxxxx 10xxxxxx
U-00010000 - U-001FFFFF:  11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
U-00200000 - U-03FFFFFF:  111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
U-04000000 - U-7FFFFFFF:  1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx

UTF-8是一种变长度的表达方式,一般UNICODE为双字节(指UCS2)但为了与以前的ASCII码兼容,ASCII为一个字节,于是就想出了这种方法,在ASCII码的范围用一个字节表示,超出ASCII码的范围就用多字节表示,这就形成了我们上面看到的UTF-8的表示方法,这样的好处是当UNICODE文档中只有ASCII码时,保存的文档都为一个字节,所以就是普通的ASCII文档无异,读入的时候也是如此,所以能与以前的ASCII文档兼容。

至于大于ASCII码的,就会由上面的第一字节的前几位表示该unicode字符的长度,比如110xxxxxx前三位的二进制表示告诉我们这是个2BYTE的UNICODE字符;1110xxxx是个三位的UNICODE字符,依此类推,而首字节后面的字节都是以10开头,见上面这是为了与ASCII码开头的0区分告诉我们这是个多字节UTF-8编码的后续位。看上面的编码,我们将上面的x部分重新连起来组成的数值就是实际的UNICODE码值了(排除10组成的标志位)。

下面是个我写的从UTF-8转换到UNICODE真实值的程序,
编译方法:
gcc utf82unicode.cpp -o utf82unicode -lstdc++

使用方法:
比如一个汉字‘新’字,它的UTF-8编码为:E696B0,为了知道他的实际UNICODE编码,执行如下程序,
 ./utf82unicode E696B0
unicode: 65B0
上面程序的输出结果告诉我们UTF8:E696B0 对应UNICODE:65B0。


附录:CPP程序utf82unicode.cpp
#include <stdio.h>
#include <string.h>

// UTF-8的unicode表示方法到unicode的值转换函数
bool utf82unicode(unsigned int  byte[], int index, int count, int& unicode)
{
/*      for (int i=index; i < count; ++i) {
                printf("byte[%d]:%0Xn",i, byte[i]);
        }
        printf("byte[index] & 0x80: %0Xn", byte[index] & 0x80);
        printf("byte[index] & 0xE0: %0Xn", byte[index] & 0xE0);
        printf("byte[index] & 0xF0: %0Xn", byte[index] & 0xF0);
*/
        if (index >= count) return false;
        if ( (byte[index] & 0x80) == 0x0)              //  一位
        {
                unicode = byte[index];
        }
         else if ((byte[index] & 0xE0) == 0xC0) // 两位
        {
                if (index + 1 >= count ) return false;
                unicode = (((int)(byte[index] & 0x1F)) << 6)
                        | (byte[ index + 1] & 0x3F);
        }
        else if ((byte[index] & 0xF0) == 0xE0) // 三位
        {
                if (index + 2 >= count) return false;
                unicode = (((int)(byte[index] & 0x0F)) << 12)
                        | (((int)(byte[index  + 1] & 0x3F)) << 6)
                        | (byte[index + 2] & 0x3F);
        }
         else if ((byte[index] & 0xF8) == 0xF0) // 四位
        {
                if (index + 3 >= count) return false;
                unicode = (((int)(byte[index] & 0x07)) << 18)
                        | (((int)(byte[index + 1] & 0x3F)) << 12)
                        | (((int)(byte[index + 2] & 0x3F)) << 6)
                        | (byte[index + 3] & 0x3F);
        }
         else if ((byte[index] & 0xFC) == 0xF8) // 五位
        {
                if (index + 4 >= count) return false;
                unicode = (((int)(byte[index] & 0x03)) << 24)
                        | (((int)(byte[index + 1] & 0x3F)) << 18)
                        | (((int)(byte[index + 2] & 0x3F)) << 12)
                        | (((int)(byte[index + 3] & 0x3F)) << 6)
                        | (byte[index + 4] & 0x3F);
        }
         else if ((byte[index] & 0xFE) == 0xFC) // 六位
        {
                if (index + 5 >= count) return false;
                unicode = (((int)(byte[index] & 0x01)) << 30)
                        | (((int)(byte[index + 1] & 0x3F)) << 24)
                        | (((int)(byte[index + 2] & 0x3F)) << 18)
                        | (((int)(byte[index + 3] & 0x3F)) << 12)
                        | (((int)(byte[index + 4] & 0x3F)) << 6)
                        | (byte[index + 5] & 0x3F);
        }
         else
         {
                return false;
        }
        return true;

}

bool char2digist(char in, char&out)
{
        if ('0' <= in && in <= '9')
                out = in - '0' + 0x0;
        else if ('A' <= in && in <= 'F')
                out = in - 'A' + 0xA;
        else if ('a' <= in && in <= 'f')
                out = in - 'a' + 0xa;
        else 
                return false;

        return true;

}

bool widechar2hexbyte(char* ch, int index, int count, unsigned int& byte)
{
        char h, l;
        if (index + 1 < count) {
                if (char2digist(ch[index], h) && char2digist(ch[index + 1], l))
                {
                        byte = ((unsigned int)(h << 4)) | l;
                        return true;
                }
        } else {
                if (char2digist(ch[index], l))
                {
                        byte = l;
                        return true;
                }
        }
        return false;

}

int main(int argc, char* argv[])
{
        int bi, i, len, unicode;
        char* hex;
        unsigned int bytes[10];
        if (argc < 2) {
                printf("usage: utf82unicode [hex string]n");
                return 1;
        }
        bi = 0, len = strlen(argv[1]);
//      printf("argv[1]:%s,len:%dn", argv[1], len);
        for (int i = 0; i < len && bi < 10; ++ i)
        {
                if (!widechar2hexbyte(argv[1], i++, len, bytes[bi++]))
                        return 1;
        }
        unicode = 0;
        if (utf82unicode(bytes, 0, bi, unicode))
        {
                printf("unicode: %0Xn", unicode);
                return 0;
        }
        return 1;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值