我想从 binary.txt 文件创建一个 binary.bin 文件。
.txt 文件仅包含 1 和 0。我找到了一种使用 xxd linux 函数将十六进制转换为 .bin 的方法,但找不到将 .txt 零和一转换为十六进制的方法,该方法可以使用 xxd 工具生成 .bin 文件。
我试图制作一个脚本,用于将 binary.txt 转换为 hexademical.txt,然后可以在 linux 中与 xxd 一起使用,但这似乎不起作用......问题是 python 脚本输出了一些其他的十六进制而不是linux 中的 xxd hexdump 函数。
简而言之...我尝试使用带有 .bin 文件的 xxd 进行此操作:
.bin > 十六进制 > .bin (有效)
我无法从以下位置制作 .bin 文件:
十六进制(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