十六进制转储为二进制-等效xxd -r [英] hexadecimal dump to binary - xxd -r equivalent

查看:570
本文介绍了十六进制转储为二进制-等效xxd -r的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux bash shell中,我使用以下命令将纯十六进制转储转换为二进制

In Linux bash shell, I use the following to convert a plain hexadecimal dump into binary

$ echo "8cd59ef53c9aaa68311b73767e0975e7" | xxd -r -p > xxd_out.bin

当我在文本查看器中打开文件时,它看起来像ŒÕžõ<šªh1sv~ uç

when I open the file in text viewer it looks like ŒÕžõ<šªh1sv~ uç

或在xxd

$ xxd -b xxd_out.bin
00000000: 10001100 11010101 10011110 11110101 00111100 10011010  ....<.
00000006: 10101010 01101000 00110001 00011011 01110011 01110110  .h1.sv
0000000c: 01111110 00001001 01110101 11100111                    ~.u.

或在Notepad ++ Hex-Editor(插件)视图中

or in Notepad++ Hex-Editor (plugin) view

如何在Ruby中获得相同的二进制输出?有没有可用的库执行xxd -r -p的工作?

How can I get the same binary output in Ruby ? Is there any library available which does what xxd -r -p would do ?

推荐答案

使用 Array#pack

.scan(/../)会将"8cd59e"拆分为["8c","d5","9e"]

.map(&:hex)会将其转换为[0x8c, 0xd5, 0x9e]

.pack("c*")会将其打包到"\x8c\xd5\x9e"

echo "8cd59ef53c9aaa68311b73767e0975e7" | \
  ruby -ne 'print $_.scan(/../).map(&:hex).pack("c*")' | \
  xxd -b

输出:

00000000: 10001100 11010101 10011110 11110101 00111100 10011010  ....<.
00000006: 10101010 01101000 00110001 00011011 01110011 01110110  .h1.sv
0000000c: 01111110 00001001 01110101 11100111                    ~.u.

这篇关于十六进制转储为二进制-等效xxd -r的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆