3

I can convert Unit8Array to hex by using below code

 var bkh = {
    publicKey: new Uint8Array([91, 221, 234, 40, 144, 246, 91, 187, 154, 76,
        60, 178, 204, 81, 35, 195, 254, 114, 246, 88, 90, 170, 68, 97, 199,
        170, 72, 36, 107, 66, 206, 9]
    ),

    secretKey: new Uint8Array([64, 68, 196, 103, 210, 179, 166, 40, 187,
        150, 167, 233, 144, 206, 64, 26, 77, 133, 70, 238, 232, 227, 133,
        83, 149, 202, 213, 41, 152, 243, 237, 41]
    )
}

let hex = Buffer.from(bkh.publicKey).toString('hex');

console.log('master key',hex)

How can I convert this hex value back to Unit8Array in node

Vinayak B
  • 4,430
  • 4
  • 28
  • 58
  • Possible duplicate of [How to convert a hexadecimal string to Uint8Array and back in JavaScript?](https://stackoverflow.com/questions/38987784/how-to-convert-a-hexadecimal-string-to-uint8array-and-back-in-javascript) – Brian Adams Mar 20 '19 at 12:34

1 Answers1

7
var hex = '5bddea2890f65bbb9a4c3cb2cc5123c3fe72f6585aaa4461c7aa48246b42ce09'
new Uint8Array(Buffer.from(hex, 'hex'))
// or 
Uint8Array.from(Buffer.from(hex, 'hex'))
Teneff
  • 30,564
  • 13
  • 72
  • 103
  • 1
    I voted to close this question since it's a duplicate of [this one](https://stackoverflow.com/q/38987784/10149510), but this answer is better than the ones on that question and really should be added there as well. – Brian Adams Mar 20 '19 at 12:39
  • @brian-lives-outdoors feel free to add it :) – Teneff Mar 20 '19 at 12:40
  • [done](https://stackoverflow.com/a/55263004/10149510)...including a link back to this answer as the source – Brian Adams Mar 20 '19 at 14:28