将文件转换为HEX String Python

Zac*_*own 25 python string hex file

如何使用Python将文件转换为HEX字符串?我已经在谷歌搜索了这个,但似乎找不到任何有用的东西.

unu*_*tbu 48

import binascii
filename = 'test.dat'
with open(filename, 'rb') as f:
    content = f.read()
print(binascii.hexlify(content))
Run Code Online (Sandbox Code Playgroud)

  • 这会转换png,jpg和其他图像格式文件吗? (2认同)
  • 在 Ubuntu 上,你可以通过运行 `lscpu` 来检查你机器的字节序。要获得 hexdump 输出以匹配上面代码的输出,请运行 [`hexdump -C /path/to/file`](https://unix.stackexchange.com/a/55772/3330)。 (2认同)