2

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)));
}
6
  • 2
    2e65 should be two bytes, not four. Commented Mar 8, 2020 at 18:31
  • 2
    You can use hexStringToByteArr[i]=Character.digit(c,16); instead of all the if statements. Commented Mar 8, 2020 at 18:32
  • @ElliottFrisch could you tell me why I need two instead of four? i did what said with the dividing by 2 but it causes my program to have an error. Also I do not understand logically, if my string has 4 values, would I not need the same amount of indexes in my byte array to fit them in? Commented Mar 8, 2020 at 18:42
  • One byte is 2 hex digits, if it were one digit it would be constrained to sixteen values (hence hex). Commented Mar 8, 2020 at 18:44
  • Incase I didnt write my question correctly. My code is working as intended with the output. I need every single hexadecimal character to be displayed in its decimal form in a byte array Commented Mar 8, 2020 at 18:45

3 Answers 3

2

"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]
Sign up to request clarification or add additional context in comments.

1 Comment

The second output is what I was asking for. Thanks for providing both.
0

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];

2 Comments

why must byte arrays be half the length? wouldnt I need a byte array of 4 indexes since i have 4 values? If i include byte[] hexStringToByteArr = new byte[dataString.length() / 2]; i get an out of bounds of array error
@wetmoney You get the ArrayIndexOutOfBounds error because your code tries to access indexes of the byte array that are beyond half the string length. Use my code instead (so the for loop included) and it will work.
0

Your big block o' ifs can be replaced with:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.