tomatofighter
tomatofighter

Reputation: 69

converting hexadecimal string to byte array

I wrote a program that converts a hexadecimal string into a byte array, eg "2e65" produces [2,14,6,5].

Is there a way to do it with less lines of code or without using the ASCII table values.

public static byte[] stringToData( String dataString) {
    byte [] hexStringToByteArr = new byte[dataString.length()];
    for ( int i = 0; i < dataString.length(); i++) {
        char c = dataString.charAt(i);
        if ( c == 48 ) { hexStringToByteArr[i] = 0;}
        if ( c == 49 ) { hexStringToByteArr[i] = 1;}
        if ( c == 50 ) { hexStringToByteArr[i] = 2;}
        if ( c == 51 ) { hexStringToByteArr[i] = 3;}
        if ( c == 52 ) { hexStringToByteArr[i] = 4;}
        if ( c == 53 ) { hexStringToByteArr[i] = 5;}
        if ( c == 54 ) { hexStringToByteArr[i] = 6;}
        if ( c == 55 ) { hexStringToByteArr[i] = 7;}
        if ( c == 56 ) { hexStringToByteArr[i] = 8;}
        if ( c == 57 ) { hexStringToByteArr[i] = 9;}
        if ( c == 97 ) { hexStringToByteArr[i] = 10;}
        if ( c == 98 ) { hexStringToByteArr[i] = 11;}
        if ( c == 99 ) { hexStringToByteArr[i] = 12;}
        if ( c == 100 ) {hexStringToByteArr[i] = 13;}
        if ( c == 101 ) {hexStringToByteArr[i] = 14;}
        if ( c == 102 ) {hexStringToByteArr[i] = 15;}
    }
    return hexStringToByteArr;
}

public static void main(String [] args) {
    String pracString = "2e65";    
    System.out.println(Arrays.toString(stringToData(pracString)));
}

Upvotes: 2

Views: 903

Answers (3)

Jacob G.
Jacob G.

Reputation: 29680

"2e65" in hexadecimal represents two bytes (2e, which corresponds to 46 in decimal, and 65, which corresponds to 101 in decimal). To get a byte[] containing two bytes, you can utilize BigInteger:

String hex = "2e65";
byte[] b = new BigInteger(hex, 16).toByteArray();
System.out.println(Arrays.toString(b));

The output of the above snippet is:

[46, 101]

If, instead, you want to convert each of the four hexadecimal digits to a byte and store them in a byte[], then you can use Character#digit while iterating over each char in the String:

String hex = "2e65";
byte[] b = new byte[hex.length()];

for (int i = 0; i < b.length; i++) {
    b[i] = (byte) Character.digit(hex.charAt(i), 16);
}

System.out.println(Arrays.toString(b));

The output of the above snippet is:

[2, 14, 6, 5]

Upvotes: 2

Bohemian
Bohemian

Reputation: 424973

Your big block o' ifs can be replaced with:

hexStringToByteArr[i] = c < 58 ? c - 48 : c - 87;

Upvotes: 0

Max Voisard
Max Voisard

Reputation: 1942

To convert hex string to byte array, you need to first get the length of the given string and include it while creating a new byte array.

string str = "2e65";
byte[] val = new byte[str.length() / 2];

Now, take a for loop until the length of the byte array.

for (int i = 0; i < val.length; i++) {
   int index = i * 2;
   int j = Integer.parseInt(str.substring(index, index + 2), 16);
   val[i] = (byte) j;
}

So, since byte arrays must be half the length, change this line

byte[] hexStringToByteArr = new byte[dataString.length()];

to this:

byte[] hexStringToByteArr = new byte[dataString.length() / 2];

Upvotes: 0

Related Questions