'How to convert hex string to byte array in Java

I want to convert an hex string to byte array, I know these type of questions are already asked, I have tried their solution, But these solutions are not working with the library I'm using, I'm trying to convert the string

0x049e520ba27332ffed59ec7a34f744d4acd86ad22081d16f851d9f355ac09a70bdc14219c33ba7f5df62391a6f0f067c571bd6c03e83f81cd166e55fe043426a1c

To an byte array but the byte array I get is as

[48, 120, 48, 52, 57, 101, 53, 50, 48, 98, 97, 50, 55, 51, 51, 50, 102, 102, 101, 100, 53, 57, 101, 99, 55, 97, 51, 52, 102, 55, 52, 52, 100, 52, 97, 99, 100, 56, 54, 97, 100, 50, 50, 48, 56, 49, 100, 49, 54, 102, 56, 53, 49, 100, 57, 102, 51, 53, 53, 97, 99, 48, 57, 97, 55, 48, 98, 100, 99, 49, 52, 50, 49, 57, 99, 51, 51, 98, 97, 55, 102, 53, 100, 102, 54, 50, 51, 57, 49, 97, 54, 102, 48, 102, 48, 54, 55, 99, 53, 55, 49, 98, 100, 54, 99, 48, 51, 101, 56, 51, 102, 56, 49, 99, 100, 49, 54, 54, 101, 53, 53, 102, 101, 48, 52, 51, 52, 50, 54, 97, 49, 99]

But the library I'm using gives an error as Invalid Key, Because the library excepts the byte array to be as

[4 158 82 11 162 115 50 255 237 89 236 122 52 247 68 212 172 216 106 210 32 129 209 111 133 29 159 53 90 192 154 112 189 193 66 25 195 59 167 245 223 98 57 26 111 15 6 124 87 27 214 192 62 131 248 28 209 102 229 95 224 67 66 106 28]

How can I do this in Java, I don't know what the encoding is or how the byte array is being calculated

This is the code

String publicKey = "0x049e520ba27332ffed59ec7a34f744d4acd86ad22081d16f851d9f355ac09a70bdc14219c33ba7f5df62391a6f0f067c571bd6c03e83f81cd166e55fe043426a1c
";

NewMessage message1 = Geth.newNewMessage();

message1.setPayload(message.getBytes());
message1.setPublicKey(WhisperHelper.hexStringToByteArray(publicKey));

public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }



Solution 1:[1]

This function does what you want

public static byte[] getByteArray(String str) {
    if (!"0x".equals(str.substring(0,2))) {
        return null; // or throw some exception
    } else {
        String tmp = str.substring(2, str.length()-2);
        int bytes = tmp.length() / 2;
        byte[] array = new byte[bytes];

        for (int i = 0; i < bytes; i++) {
            byte b = (byte) ((Character.digit(tmp.charAt(i*2), 16) << 4)
                + Character.digit(tmp.charAt(i*2+1), 16));
            array[i] = b;
        }

        return array;
    }
}

except that numbers greater than 127 (0x7F) will be represented as negative numbers. This is because bytes in jave are signed.

So the out put will be

[4 -98 82 11 -94 ...

Solution 2:[2]

Here is my code that showed the best performance...

    public static byte[] hex2bytes(String str) {
        if( str == null || str.length() == 0 )
            return null;

        byte[] hex = new byte[str.length() / 2];
        int tmp1, tmp2;

        for(int i=0; i < hex.length; i++) {
            tmp1 = str.charAt(i*2);
            tmp2 = str.charAt(i*2+1);
            hex[i] = (byte)( (tmp1 < 0x41 ? (tmp1 & 0x0F) << 4 : ((tmp1 & 0x0F) + 9) << 4)
                    | (tmp2 < 0x41 ? tmp2 & 0x0F : (tmp2 & 0x0F) + 9) );
        }

        return hex;
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ackdari
Solution 2 spectator