首页  

字符集编码基础知识     所属分类 architecture 浏览量 816
BOM头 Byte Order Mark

文本文件开头

UTF8            0xEF 0xBB 0xBF
Unicode大端模式  0xFE 0xFF
Unicode小端模式  0xFF 0xFE

无BOM  如何区分 UTF8 GBK

标准ASCII  128个字符 0~127
0x00~0x7F(01111111)

ASCII 高字节的最高位为0

Unicode  
0x0000 到 0x007F  单字节UTF8
0x0080 到 0x07FF  双字节UTF8
0x8000 到 0xFFFF  三字节UTF8  一般中文在这个范围

GBK是中国标准,只在中国使用
各国有各自的编码标准,互不兼容
国际组织发行了一个全球统一编码表,把各国文字都统一在一个编码标准里,名为Unicode


UTF8 编码规则
单字节字符,字节的第一位设为0,后面7位为这个符号的 Unicode 码。英语字母,UTF-8 编码和 ASCII 码是相同的
n字节字符(n > 1),第一个字节的前n位都设为1,第n + 1位设为0,后面字节的前两位一律设为10 , 剩下为该字符的 Unicode 码

1字节 0xxxxxxx 
2字节 110xxxxx 10xxxxxx 
3字节 1110xxxx 10xxxxxx 10xxxxxx 
4字节 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 
5字节 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 
6字节 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 

byte 转 二进制

public static String byteToBit(byte b) {		
    StringBuilder sb = new StringBuilder(8);
    sb.append((byte) ((b >> 7) & 0x1));
    sb.append((byte) ((b >> 6) & 0x1));
    sb.append((byte) ((b >> 5) & 0x1));
    sb.append((byte) ((b >> 4) & 0x1));
    sb.append((byte) ((b >> 3) & 0x1));
    sb.append((byte) ((b >> 2) & 0x1));
    sb.append((byte) ((b >> 1) & 0x1));
    sb.append((byte) ((b >> 0) & 0x1));
    return sb.toString();
}


StandardCharsets
US_ASCII
ISO_8859_1
UTF_8



ISO-8859-1 hi中国 104 105 63 63 01101000 01101001 00111111 00111111 US-ASCII hi中国 104 105 63 63 01101000 01101001 00111111 00111111 GBK hi中国 104 105 -42 -48 -71 -6 01101000 01101001 11010110 11010000 10111001 11111010 UTF8 hi中国 104 105 -28 -72 -83 -27 -101 -67 01101000 01101001 11100100 10111000 10101101 11100101 10011011 10111101
完整代码 https://gitee.com/dyyx/hellocode/blob/master/src/util/CharsetUtil.java

上一篇     下一篇
HTTPS连接过程及中间人攻击防范

xxlrpc例子说明

xxl-job 使用简介

时间管理法则20条

做好项目管理的七个技巧

BlockingQueue add offer put 区别