简体   繁体   English

在 Javascript 中按字节对 base64->buffer 十六进制字符串进行分组?

[英]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.

相关问题 Node.js 6.10十六进制-> BASE64-> UTF8 - Node.js 6.10 HEX->BASE64->UTF8 JavaScript:从图像中删除字节(base64字符串) - JavaScript: Remove bytes from image (base64 String) 用于 JavaScript 的 HEX 到 Base64 转换器 - HEX to Base64 converter for JavaScript 在JavaScript中将Image Hex转换为base64 - Converting Image Hex to base64 in JavaScript Base 64到字符串Javascript - Base 64 to string Javascript 如何在不使用 JavaScript 中的内置 function 的情况下将 base64 转换为十六进制字符串 - How to convert base64 to hex string without using builtin function in JavaScript 在 node js / javascript 中使用 OR 条件时,传入的参数必须是 Buffer 或 12 个字节的字符串或 24 个十六进制字符的字符串 - Argument passed in must be a Buffer or string of 12 bytes or a string of 24 hex characters while using OR condition in node js / javascript 这个正在解码十六进制字符串然后将其编码为 base64 的 Python 代码将如何写入 Javascript? - How would this Python code that is decoding a hex string and then encoding it to base64 be written in Javascript? JavaScript比较缓冲区和Python之间的十六进制字符串 - JavaScript compare hex string from buffer to Python 在Javascript和C#中将base64编码的字节转换为不同的字符串 - Converting base64 encoded bytes to string different in Javascript and C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM