小彩龟会互咬吗?:用java程序将GBK字符转成UTF-8编码格式

来源:百度文库 编辑:偶看新闻 时间:2024/04/28 22:43:19

          UTF 代表 UCS Transformation Format.

UTF-8 采用变长度字节来表示字符,理论上最多可以到 6 个字节长度(一个字符六个字节)。

UTF-8 编码兼容了 ASC II(0-127), 也就是说 UTF-8 对于 ASC II 字符的编码是和 ASC II 一样的。

对于超过一个字节长度的字符,才用以下编码规范:

左边第一个字节1的个数表示这个字符编码字节的位数,

例如两位字节字符编码样式为为:110xxxxx 10xxxxxx;

三位字节字符的编码样式为:1110xxxx 10xxxxxx 10xxxxxx.;

以此类推,六位字节字符的编码样式为:1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx。

xxx 的值由字符编码的二进制表示的位填入。只用最短的那个足够表达一个字符编码的多字节串。

例如:

Unicode 字符: 00 A9(版权符号) = 1010 1001,

UTF-8 编码为:11000010 10101001 = 0x C2 0xA9;

字符 22 60 (不等于符号) = 0010 0010 0110 0000,

UTF-8 编码为:11100010 10001001 10100000 = 0xE2 0x89 0xA0

代码

  1. package com.lang.string;   
  2.   
  3. public class ConverFromGBKToUTF8 {   
  4.  public static void main(String[] args){   
  5.      
  6.   try {   
  7.       
  8.          ConverFromGBKToUTF8 convert = new ConverFromGBKToUTF8();   
  9.          byte [] fullByte = convert.gbk2utf8(chenese);   
  10.          String fullStr = new String(fullByte, "UTF-8");   
  11.          System.out.println("string from GBK to UTF-8 byte:  " + fullStr);   
  12.   
  13.      } catch (Exception e) {   
  14.       e.printStackTrace();   
  15.      }   
  16.  }   
  17.     
  18.  public byte[] gbk2utf8(String chenese){   
  19.   char c[] = chenese.toCharArray();   
  20.         byte [] fullByte =new byte[3*c.length];   
  21.         for(int i=0; i
  22.          int m = (int)c[i];   
  23.          String word = Integer.toBinaryString(m);   
  24. //         System.out.println(word);   
  25.             
  26.          StringBuffer sb = new StringBuffer();   
  27.          int len = 16 - word.length();   
  28.          //补零   
  29.          for(int j=0; j
  30.           sb.append("0");   
  31.          }   
  32.          sb.append(word);   
  33.          sb.insert(0, "1110");   
  34.          sb.insert(8, "10");   
  35.          sb.insert(16, "10");   
  36.             
  37. //         System.out.println(sb.toString());   
  38.             
  39.          String s1 = sb.substring(0,8);             
  40.          String s2 = sb.substring(8,16);             
  41.          String s3 = sb.substring(16);   
  42.             
  43.          byte b0 = Integer.valueOf(s1, 2).byteValue();   
  44.          byte b1 = Integer.valueOf(s2, 2).byteValue();   
  45.          byte b2 = Integer.valueOf(s3, 2).byteValue();   
  46.          byte[] bf = new byte[3];   
  47.          bf[0] = b0;   
  48.          fullByte[i*3] = bf[0];   
  49.          bf[1] = b1;   
  50.          fullByte[i*3+1] = bf[1];   
  51.          bf[2] = b2;   
  52.          fullByte[i*3+2] = bf[2];   
  53.             
  54.         }   
  55.         return fullByte;   
  56.  }   
  57. }  

 

UTF-8的编码原理和特性:

U+0000~U+007E 1 _ _ _ _ _ _ _ (7bits)

U+0080~U+07FF 1 1 0_ _ _ _ _ 1 0_ _ _ _ _ _ (11bits)

U+0800~U+FFFF 1 1 1 0 _ _ _ _ 1 0 _ _ _ _ _ _ 1 0 _ _ _ _ _ _ (16bits)

php中关于字符GBK转utf-8 在vb中如何编写程序使得utf-8编码转为gbk编码 php中utf-8编码改成gbk的方法 GBK编码和UTF-8会不会起冲突 用Java写彩信发送程序。引用getstyle方法把信息转utf-8,终端收到信息最后一个字出现乱码 gbk字符转换为中文 如何将UTF-8转换为ASCI? PHP中如何把输出的文字转成UTF-8 请问Discuz! 4.0.0 GBK 简体中文版(含英文)与Discuz! 4.0.0 UTF-8 简体中文版(含英文)是什么区别? 在ASP编程中,如何将"转换"形式的UTF-8字符还原回汉字呢,要用到什么函数? 用ASP程序怎样生成UTF-8格式的HTML,求高手指点。 为什么“百度-知道”栏目不支持GBK字符?? 为什么“百度-知道”栏目不支持GBK字符?? UTF-8编码的ASP网页 POST中文字符时出现乱码 请问JAVA如何将字符型转换为整型? JAVA题目:将命令行第一个参数每个字符放入一字符数组,然后将字符逆序输出。 utf-8和gb2312有什么不一样,什么时候用utf-8,又什么时候用gb2312???急 编写字符界面的Java Application ,将用户输入的小写字符转化成大写字符打印在屏幕上, gbk mfc application 遇到问题将关闭? 如何将Java程序放进手机里 如何将java程序制作成.exe 有没有能将java程序打包生成可执行文件的工具?用jbuider9编写的程序! JAVA程序的数据库处理部分在测试正常,但用InstallAnywhere转成exe文件后失效? UTF-8编码是怎么回事?