3

Can anyone please tell me equivalent Nodejs code to convert hex string to byte array which is in Java

public static byte[] hexStringToByteArray(String s) {
    byte[] b = new byte[s.length() / 2];
    for (int i = 0; i < b.length; i++) {
        int index = i * 2;
        int v = Integer.parseInt(s.substring(index, index + 2), 16);
        b[i] = (byte) v;

    }
    return b;
}

1 Answer 1

19

You can use Buffer.from(str, [encoding]) to perform the conversion.

Buffer.from(str, 'hex');
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.