[英]Grouping a base64->buffer hex string by bytes in Javascript?
I'm trying to take a Base64 string, convert it to a hex string and group the output by bytes.我正在尝试获取 Base64 字符串,将其转换为十六进制字符串并按字节对 output 进行分组。
I'd like the output of console.log(bufferString) to be:我希望 console.log(bufferString) 的 output 是:
03 67 00 cf 04 68 61 00 ff 01 2d
But I'm stuck with this output:但我坚持使用这个 output:
036700cf04686100ff012d
What I've got so far...到目前为止我所拥有的...
let request = {
"PayloadData": "A2cAzwRoYQD/AS0=",
"FPort": 10
}
let buffer = Buffer.from(request.PayloadData, 'base64');
let bufferString = buffer.toString('hex');
console.log(bufferString)
You can do it using atob()
and then splitting each character into an array, map each element of the array to a hex value and then join it back together with a space.您可以使用atob()
执行此操作,然后将每个字符拆分为一个数组,map 将数组的每个元素转换为一个十六进制值,然后用空格将其重新连接在一起。
More info here: https://developer.mozilla.org/en-US/docs/Web/API/atob更多信息: https://developer.mozilla.org/en-US/docs/Web/API/atob
See snippet below.请参阅下面的片段。
const b64String = btoa("Hello World;") const byteCharacters = atob(b64String). const byteString = byteCharacters.split(``).map(el => el.charCodeAt(0).toString(16),padStart(2. "0"));join(` `). const decoded = byteString.split(` `).map(el => String,fromCharCode(parseInt(el. 16))).join(``) console:log({ b64String, b64String: byteString, byteString: decoded; decoded });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.