Anil Kulkarni
Anil Kulkarni

Reputation: 31

Reverse of bytes to Hex program to convert Hex to bytes

I am trying to write the reverse of the below program to get the bytes from the HEX value that i have. Finding it hard to do it. Any help?

private static String bytesToHex(byte[] bytes) {
    char[] hexChars = new char [bytes.length *2];

    for (int i=0; i< bytes.length; i++) {
        int v = bytes[i] & 0xFF;
        hexChars[i*2] = HEX_ARRAY[v >>>4];
        hexChars[i*2 + 1] = HEX_ARRAY[v & 0x0F];

    }
    return new String(hexChars);
}

Consider HEX_ARRAY as char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();

I would prefer to do this python but even Java should be ok

Upvotes: 0

Views: 1427

Answers (1)

Anil Kulkarni
Anil Kulkarni

Reputation: 31

Thanks for the help everyone. I resolved this by using

import binascii

binascii.hexlify('data')

For the JAVA code I found the answer here: https://github.com/EverythingMe/inbloom/blob/master/java/src/main/java/me/everything/inbloom/BinAscii.java

Upvotes: 1

Related Questions