Binary byte and hexadecimal conversion in Java

Keywords: Programming Java

In Java, there are two main ideas about the conversion between byte and hexadecimal.

1. When A binary byte is converted to hexadecimal, it is required to "&" the high bit of the byte and 0xF0, and then move 4 bits to the left to get the hexadecimal A of the high bit of the byte; it is required to "&" the low bit of the byte and 0x0F to get the low hexadecimal B, and the combination of two hexadecimal numbers into A block AB is the hexadecimal representation of the byte.
2. When converting hexadecimal to binary byte, move the decimal digit corresponding to hexadecimal character to 4 to the right to get the high-order A of byte, and do "|" operation between the decimal digit B corresponding to the low-order hexadecimal character of byte and A to get the binary byte representation of hexadecimal.

The Java code I tested is as follows:

Java code collection code
public class Test01 {

private static String hexStr =  "0123456789ABCDEF";  
private static String[] binaryArray =   
    {"0000","0001","0010","0011",  
    "0100","0101","0110","0111",  
    "1000","1001","1010","1011",  
    "1100","1101","1110","1111"};  
      
public static void main(String[] args)   
{  
    String str = "Binary and hexadecimal conversion test";  
    System.out.println("Source string:\n"+str);  
      
    String hexString = BinaryToHexString(str.getBytes());  
    System.out.println("Convert to hex:\n"+hexString);  
    System.out.println("Convert to binary:\n"+bytes2BinaryStr(str.getBytes()));  
      
    byte [] bArray = HexStringToBinary(hexString);  
    System.out.println("take str Convert hex file to binary and then to String: \n"+new String(bArray));  
  
}  
/** 
 *  
 * @param str 
 * @return Convert to binary string 
 */  
public static String bytes2BinaryStr(byte[] bArray){  
      
    String outStr = "";  
    int pos = 0;  
    for(byte b:bArray){  
        //Top four digit  
        pos = (b&0xF0)>>4;  
        outStr+=binaryArray[pos];  
        //Low four bit  
        pos=b&0x0F;  
        outStr+=binaryArray[pos];  
    }  
    return outStr;  
      
}  
/** 
 *  
 * @param bytes 
 * @return Convert binary to hexadecimal character output 
 */  
public static String BinaryToHexString(byte[] bytes){  
      
    String result = "";  
    String hex = "";  
    for(int i=0;i<bytes.length;i++){  
        //Byte high 4 bits  
        hex = String.valueOf(hexStr.charAt((bytes[i]&0xF0)>>4));  
        //Byte low 4 bits  
        hex += String.valueOf(hexStr.charAt(bytes[i]&0x0F));  
        result +=hex+" ";  
    }  
    return result;  
}  
/** 
 *  
 * @param hexString 
 * @return Convert hex to byte array 
 */  
public static byte[] HexStringToBinary(String hexString){  
    //The length of hexString is rounded to 2 as the length of bytes  
    int len = hexString.length()/2;  
    byte[] bytes = new byte[len];  
    byte high = 0;//Byte high 4 bits  
    byte low = 0;//Byte low 4 bits  

    for(int i=0;i<len;i++){  
         //Move four places to the right to get the high position  
         high = (byte)((hexStr.indexOf(hexString.charAt(2*i)))<<4);  
         low = (byte)hexStr.indexOf(hexString.charAt(2*i+1));  
         bytes[i] = (byte) (high|low);//To do or operate on high ground.  
    }  
    return bytes;  
}  

}

The output after operation is as follows:

Source string:

Binary and hexadecimal conversion test

Convert to hex:

E4BA8CE8BF9BE588B6E4B88EE58D81E585ADE8BF9BE588B6E4BA92E8BDACE6B58BE8AF95

Convert to binary:

11100100 10111010 10001100 11101000 10111111 10011011 11100101 10001000 10110110 11100100 10111000 10001110 11100101 10001101 10000001 11100101 10000101 10101101 11101000 10111111 10011011 11100101 10001000 10110110 11100100 10111010 10010010 11101000 10111101 10101100 11100110 10110101 10001011 11101000 10101111 10010101

Convert the hexadecimal file of str to binary and String:

Binary and hexadecimal conversion test

Posted by donald on Fri, 01 Nov 2019 23:48:09 -0700