如何从二进制 .txt 文件创建 .bin 文件?

问题描述

我想从 binary.txt 文件创建一个 binary.bin 文件。

.txt 文件只包含 1 和 0。我找到了一种使用 xxd linux 函数将十六进制转换为 .bin 的方法,但找不到一种将 .txt 零和 1 转换为十六进制的方法,该方法可以使用 xxd 工具来制作 .bin 文件。

我尝试制作一个脚本,将 binary.txt 转换为 hexademical.txt,然后可以在 linux 中与 xxd 一起使用,但这似乎不起作用......问题是 python 脚本输出了一些其他的十六进制比linux中的xxd hexdump函数。

简而言之...我尝试使用 xxd 和 .bin 文件:

.bin > hex > .bin(有效)

我无法从以下位置制作 .bin 文件:

hex (python) > .bin(不起作用)

# converts binary.txt > hex .txt
# for example: 01000110 outputs 0xf42ae

data1 = open('13bitSystem_1row.txt','r')
data2 = open("hex_" + data1.name,'w')

# () specifies number of decimal characters used,remove number to run whole file
file = data1.read()

decimal_representation = int(file)
hexadecimal_string = hex(decimal_representation)

data2.write(hexadecimal_string)

data1.close()
data2.close()

例如:二进制序列 .txt 01000110 在 xxd 中输出 4F 而在 python 中输出 0xf42ae

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)