lt1988 发表于 2013-1-14 23:44:52

java char and UTF

java中的char 占两个字节,默认使用UNICODE?转换成文件保存时,根据字符范围写入到文件中。
0开使的字符为asc码的128个字符,用1byte保存;x<=0X7F
110开始的字符用2byte保存;0X7F<x<0X8F
111开始的字符用3byte保存。x>0X8F
不多说,上一段hsqldb里的代码。
public static int stringToUTFBytes(String str,                                       HsqlByteArrayOutputStream out) {      int strlen = str.length();      int c,            count= 0;      if (out.count + strlen + 8 > out.buffer.length) {            out.ensureRoom(strlen + 8);      }      char[] arr = str.toCharArray();      for (int i = 0; i < strlen; i++) {            c = arr;            if (c >= 0x0001 && c <= 0x007F) {                out.buffer = (byte) c;                count++;            } else if (c > 0x07FF) {                out.buffer = (byte) (0xE0 | ((c >> 12) & 0x0F));                out.buffer = (byte) (0x80 | ((c >> 6) & 0x3F));                out.buffer = (byte) (0x80 | ((c >> 0) & 0x3F));                count                   += 3;            } else {                out.buffer = (byte) (0xC0 | ((c >> 6) & 0x1F));                out.buffer = (byte) (0x80 | ((c >> 0) & 0x3F));                count                   += 2;            }            if (out.count + 8 > out.buffer.length) {                out.ensureRoom(strlen - i + 8);            }      }      return count;    } 
页: [1]
查看完整版本: java char and UTF