String-Character-Binary Relation

 

An example is used to illustrate how Chinese characters are converted into corresponding bytes.

Chinese encoding is UNIQUE encoding, that is, 2 bytes identify a Chinese character;

 

For example: i = byte[] bs3 = new byte[]{-50,-46} = GBK encoding decimal is 52946.

 

Such as:

String str2 = "我";

byte[] bs2 = str2.getBytes("gbk");

The bytes I encoded with gbk are: byte[] bs2 = new byte[]{-50,-46} 

 

What did the intermediate computer do! 

1 i converted to the character "i"

2 Find the corresponding encoding table, that is, the encoding table of gbk, and then get the binary value corresponding to "I" in the encoding table,

3 A binary number is parsed as 2 bytes, the decimal for byte 1 is =-50, and the decimal for byte 2 is =-46 

4 returns an array of two bytes = bs2 

 

Convert bs2 to the Chinese character "I":

1 Calculate the binary value corresponding to -50="11001110" (complement)

 Calculation method: String tString = Integer.toBinaryString((-50 & 0xFF) + 0x100).substring(1);

 Java & 0xff:https://wenku.baidu.com/view/e454c202ed630b1c59eeb5ea.html

 

2 Calculate the binary value corresponding to -46="11010010" (complement)

 Calculation method: String tString = Integer.toBinaryString((-50 & 0xFF) + 0x100).substring(1);

3 Concatenate two bytes as =1100111011010010=52946

  Conversion address: http://tool.oschina.net/hexconvert/

4 Find "I" GBK encoded decimal value is 52946, exactly equal to the above

  Chinese characters and encoding conversion: http://www.mytju.com/classcode/tools/encode_gb2312.asp

 

 

        String str2 = "我";
//      char a = str.charAt(0);
        byte[] bs = str2.getBytes("utf-8");
        byte[] bs2 = str2.getBytes("gbk");
//      -50,-46 我
        byte[] bs3 =new byte[]{-50,-46,97,1};

        System.out.println(new String( bs3,"gbk"));
        System.out.println(  "111");
        System.out.println( (-50 & 0xFF)  );
        String tString = Integer.toBinaryString((-50 & 0xFF) + 0x100);
        String tString2 = Integer.toBinaryString((-46 & 0xFF) + 0x100).substring(1);
        System.out.println(tString +" "+tString2 );

 

 org.springframework.util.ObjectUtils :对象操作的工具类,判null,isArray,containsElement,hashCode,nullSafeToString

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326020379&siteId=291194637