使用 bash:将整数的位表示写入文件
我有一个包含二进制数据的文件,我需要替换某个位置的几个字节。我想出了以下方法将 bash 定向到偏移量并告诉我它找到了我想要的位置:
dd bs=1 if=file iseek=24 conv=block cbs=2 | hexdump
现在,使用“文件”作为输出:
echo anInteger | dd bs=1 of=hextest.txt oseek=24 conv=block cbs=2
这似乎工作得很好,我可以查看中所做的更改十六进制编辑器。问题是,“anInteger”将被写为该整数的 ASCII 表示形式(这是有道理的),但我需要编写二进制表示形式。
我想为此使用 bash,并且脚本应该在尽可能多的系统上运行(我不知道目标系统是否安装了 python 或其他系统)。
如何告诉命令将输入转换为二进制(可能是十六进制)?
I have a file with binary data and I need to replace a few bytes in a certain position. I've come up with the following to direct bash to the offset and show me that it found the place I want:
dd bs=1 if=file iseek=24 conv=block cbs=2 | hexdump
Now, to use "file" as the output:
echo anInteger | dd bs=1 of=hextest.txt oseek=24 conv=block cbs=2
This seems to work just fine, I can review the changes made in a hex editor. Problem is, "anInteger" will be written as the ASCII representation of that integer (which makes sense) but I need to write the binary representation.
I want to use bash for this and the script should run on as many systems as possible (I don't know if the target system will have python or whatever installed).
How do I tell the command to convert the input to binary (possibly from a hex)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我有点晚了,但这里有一种仅使用 一个 命令将许多十进制整数转换为二进制的方法:
首先,嵌套的 < code>printf 将仅将参数转换为十六进制。诀窍是
printf
将为此处的所有参数使用相同的格式。输出将类似于\x16\x1\x9a
,最后一个字符串将由外部printf
解释,最终输出您想要的二进制数据!如果输入超过 255,有多种方法可以对其进行分割,但我没有进一步深入研究,因为它可能取决于字节顺序。
I'm a bit late, but here is a way to convert many decimal integers to binary with only one command:
First, the nested
printf
will only convert the arguments to hexadecimal. The trick isprintf
will use the same format for all the arguments here. The output will be something like\x16\x1\x9a
, and that last string will be interpreted by the outerprintf
, which will finally output the binary data you want!There are ways to split input if it exceeds 255, but I did not dig in further since it could depend on endianness.
您可以将所需的输入放入文件中,并使用 dd 的“if=”选项来准确插入所需的输入。
You might put the desired input into a file and use the "if=" option to dd to insert exactly the input you desire.
就我而言,我需要从十进制数字参数转换为实际的无符号 16 位大端值。这可能不是最有效的方法,但它有效:
In my case, I needed to go from a decimal numeric argument to the actual unsigned 16-bit big endian value. This is probably not the most efficient way, but it works:
printf
比echo
更可移植。此函数采用十进制整数并输出具有该值的字节:printf
is more portable thanecho
. This function takes a decimal integer and outputs a byte with that value:您可以使用 echo 使用十六进制或八进制发出特定字节。例如:
将打印 ascii 0 (0x30)
(-n 删除尾随换行符)
You can use echo to emit specific bytes using hex or octal. For example:
will print ascii 0 (0x30)
(-n remove trailing newline)
xxd
是更好的方法。xxd -r infile outfile
将采用 infile 中的 ASCII 十六进制值来修补 outfile,您可以通过以下方式指定 infile 中的具体位置:1FE:55AA
xxd
is the better way.xxd -r infile outfile
will take ascii hex-value in infile to patch outfile, and you can specify the specific position in infile by this:1FE:55AA
工作就像一种享受。我使用以下代码将小尾数中字节 24 处的 4 个字节替换为两个整数(1032 和 1920)。该代码不会截断文件。
再次感谢。
Worked like a treat. I used the following code to replace 4 bytes at byte 24 in little endian with two integers (1032 and 1920). The code does not truncate the file.
Thanks again.
我有一个函数可以做到这一点:
您可以使用管道或重定向来捕获结果。
I have a function to do this:
You can use piping or redirection to catch the result.
如果您愿意依赖 bc (这相当常见)
可能会有所帮助。
If you're willing to rely on bc (which is fairly common)
might help.
在 bash 中,“printf”具有“-v”选项,并且所有 shell 都有逻辑运算符。
所以这是 bash 中更简单的形式:
With bash, "printf" has the "-v" option, and all shell has logical operators.
So here is simplier form in bash :