51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 3753|回复: 1
打印 上一主题 下一主题

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

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2007-11-22 17:49:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
所谓“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);
        }
        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)

[ 本帖最后由 Kingson 于 2007-11-22 17:55 编辑 ]
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
 楼主| 发表于 2007-11-22 17:50:08 | 只看该作者
| (((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;

}
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-5-10 17:29 , Processed in 0.070950 second(s), 29 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表