.pack(“ h*”)]。

发布于 2025-01-28 01:39:28 字数 955 浏览 2 评论 0原文

在Ruby中,我使用函数.pack(“ H*”)]。pack(“ m0”)将十六进制消息字符串编码为base64字符串。

前任。

msg_hex = 8308862831031591345F010101020663602A06E300000000EF934E0C5B29E50E000000000000000000000310000000004F04010000010000
msg_hex.pack("H*")].pack("m0") => gwiGKDEDFZE0XwEBAQIGY2AqBuMAAAAA75NODFsp5Q4AAAAAAAAAAAAAAxAAAAAATwQBAAABAAA=

在JavaScript中,我正在寻找一种等效方法将消息转换为64base消息。一些答案告诉使用buffer.from(msg_hex).tostring('Base64'),但结果是不同的。

const msg_hex = 8308862831031591345F010101020663602A06E300000000EF934E0C5B29E50E000000000000000000000310000000004F04010000010000
Buffer.from(msg_hex).toString('base64') // => ODMwODg2MjgzMTAzMTU5MTM0NUYwMTAxMDEwMjA2NjM2MDJBMDZFMzAwMDAwMDAwRUY5MzRFMEM1QjI5RTUwRTAwMDAwMDAwMDAwMDAwMDAwMzEwMDAwMDAwMDA0RjA0MDAwMDAwMDEwMDAw

.pack(“ h*”)]。javascript中Ruby的的等效方法是什么?

In Ruby, I use function .pack("H*")].pack("m0") to encode a hex message string to base64 string.

ex.

msg_hex = 8308862831031591345F010101020663602A06E300000000EF934E0C5B29E50E000000000000000000000310000000004F04010000010000
msg_hex.pack("H*")].pack("m0") => gwiGKDEDFZE0XwEBAQIGY2AqBuMAAAAA75NODFsp5Q4AAAAAAAAAAAAAAxAAAAAATwQBAAABAAA=

In Javascript, I am looking for an equivalent method to convert message to 64base message. Some answers tell useing Buffer.from(msg_hex).toString('base64'), but the result is different.

const msg_hex = 8308862831031591345F010101020663602A06E300000000EF934E0C5B29E50E000000000000000000000310000000004F04010000010000
Buffer.from(msg_hex).toString('base64') // => ODMwODg2MjgzMTAzMTU5MTM0NUYwMTAxMDEwMjA2NjM2MDJBMDZFMzAwMDAwMDAwRUY5MzRFMEM1QjI5RTUwRTAwMDAwMDAwMDAwMDAwMDAwMzEwMDAwMDAwMDA0RjA0MDAwMDAwMDEwMDAw

what is the equivalent method for .pack("H*")].pack("m0") of Ruby in Javascript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔戏命师 2025-02-04 01:39:28

您几乎可以在那里提供的东西。

您只需忘记告诉buffer.from您提供的内容,如发送字符串,默认情况下它只是将其存储为字符串。但是您想要的是将源代源解码为十六进制,然后将其存储为二进制。

所以做 - >

Buffer.from(msg_hex, 'hex').toString('base64') 

IOW:您只是错过了第二个参数hex

You was nearly there with what you supplied.

You just forgot to tell Buffer.from what you was supplying, as default if you send a string it's just going to store it as a string. But what you wanted was to decode the source as hex and then store as binary.

So doing ->

Buffer.from(msg_hex, 'hex').toString('base64') 

IOW: you just missed the second parameter hex..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文