如何在Node.js中将十六进制转换为Uint8Array?

3
我可以使用以下代码将 Unit8Array 转换为十六进制:
 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)

我该如何将此 hex 值转换回 Unit8Array 在 node 中?

1个回答

7
var hex = '5bddea2890f65bbb9a4c3cb2cc5123c3fe72f6585aaa4461c7aa48246b42ce09'
new Uint8Array(Buffer.from(hex, 'hex'))
// or 
Uint8Array.from(Buffer.from(hex, 'hex'))

1
我投票关闭这个问题,因为它是一个重复的问题,与这个相同,但这个答案比那个问题上的更好,真的应该加到那里去。 - Brian Adams
@brian-lives-outdoors 可以随意添加 :) - Teneff
完成 - 包括一个指向此答案的链接作为来源。 - Brian Adams

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接