使用bash:重新写整数文件的presentation位 [英] using bash: write bit representation of integer to file

查看:105
本文介绍了使用bash:重新写整数文件的presentation位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二进制数据的文件,我需要在某个位置,以取代几个字节。我想出了以下指导的bash的偏移和告诉我,它发现我想要的地方:

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

这似乎工作就好了,我可以回顾十六进制编辑器所做的更改。问题是,anInteger将被编写为整数的ASCII重新presentation(这是有道理的),但我需要写二进制重新presentation。

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.

我要使用bash,这和脚本应该运行在尽可能多的系统尽可能(我不知道如果目标系统将有蟒蛇或任何已安装)。

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)?

推荐答案

您可以使用echo发出使用十六进制或八进制字节的具体。例如:

You can use echo to emit specific bytes using hex or octal. For example:

echo -n -e \\x30 

将打印的ASCII 0(的0x30)

will print ascii 0 (0x30)

( - N删除尾随换行符)

(-n remove trailing newline)

这篇关于使用bash:重新写整数文件的presentation位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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