import java.io.UnsupportedEncodingException;
public class Hex2Char {
public static void main(String[] args) throws UnsupportedEncodingException {
/*
* ¤ 字符的ISO8859-1的编码为A4, 10100100
* 如果看成int型则为 10100100 -> 164 ,
* 如果看成byte型则为 10100100 -> 11011100 -> -92
*/
int encoding = Integer.parseInt("A4", 16);
System.out.println(Integer.toBinaryString(encoding));//10100100
System.out.println(encoding);//164
System.out.println((byte) encoding);//-92
byte[] b = new byte[] { (byte) encoding };
System.out.println(new String(b, "ISO8859-1"));// ¤
/*
* ¤字符的GBK编码为A1E8, 1010000111101000
*/
encoding = Integer.parseInt("A1E8", 16);
System.out.println(Integer.toBinaryString(encoding));//1010000111101000
b = new byte[2];
//取高八位
b[0] = (byte) (encoding >> 8);
//取低八位
b[1] = (byte) (encoding & 0x0FF);
System.out.println(byte2Hex(b[0]) + byte2Hex(b[1]));//A1E8
System.out.println(new String(b, "GBK"));// ¤
}
private static String byte2Hex(byte value) {
return Integer.toHexString(value & 0x00FF | 0xFF00).toUpperCase().substring(2, 4);
}
}
十六进制编码转换成字符
最新推荐文章于 2025-05-28 16:28:36 发布