計算機底層編譯原理 二進制
阿新 • • 發佈:2017-08-11
class bin [] return 計算機 col ger except sys
import java.io.IOException; public class Demo07 { public static void main(String[] args) throws IOException{ //int c = ‘中‘; int c = 0x4e2d; System.out.println(c); System.out.println(Integer.toBinaryString(c)); int m = 0x3f; //將c的最後6位截取下來為k intk = c&m; System.out.println(Integer.toBinaryString(k)); //將字符c的編為UTF-8,得到最後一個字節b3 int b3 = c&0x3f|0x80; System.out.println(Integer.toBinaryString(b3)); //編碼b2 int b2 = (c>>>6)&0x3f|0x80; System.out.println(Integer.toBinaryString(b2));//編碼b1 int b1 = (c>>>12)|0xe0; //驗證: byte[] bytes = {(byte)b1,(byte)b2,(byte)b3}; String str = new String(bytes, "UTF-8"); System.out.println(str);//中 char cc = decodeUtf8(bytes); System.out.println(cc); } public static char decodeUtf8(byte[] bytes){ int b1 = bytes[0]; int b2 = bytes[1]; int b3 = bytes[2]; System.out.println(Integer.toBinaryString(b1)); //b1 = 11111111 11111111 11111111 11100010 //b2 = 11111111 11111111 11111111 10111000 //b3 = 11111111 11111111 11111111 10101101 // (b1&0xf)<<12 00000000 00000000 00100000 00000000 // (b2&0x3f)<<6 00000000 00000000 00001110 00000000 // (b3&0x3f) 00000000 00000000 00000000 00101101 // ch= 00000000 00000000 00101110 00101101 int ch = ((b1&0xf)<<12)|((b2&0x3f)<<6)|(b3&0x3f); return (char)ch; } public static byte[] utf8(char ch){ int b1 = (ch>>>12)|0xe0; int b2 = (ch>>>6) & 0x3f | 0x80; int b3 = ch & 0x3f | 0x80; return new byte[]{(byte)b1,(byte)b2,(byte)b3}; } }
計算機底層編譯原理 二進制