Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs code to convert hex string to byte array?

Tags:

java

node.js

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;
}
like image 634
Sudhanshu Gaur Avatar asked Dec 02 '25 05:12

Sudhanshu Gaur


1 Answers

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

Buffer.from(str, 'hex');
like image 98
Jake Holzinger Avatar answered Dec 03 '25 19:12

Jake Holzinger