将文件转换为十六进制字符串 Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3964245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:35:16 来源:igfitidea点击:
Convert File to HEX String Python
提问by Zac Brown
How would I convert a file to a HEX string using Python? I have searched all over Google for this, but can't seem to find anything useful.
如何使用 Python 将文件转换为十六进制字符串?我在谷歌上搜索过这个,但似乎找不到任何有用的东西。
采纳答案by unutbu
import binascii
filename = 'test.dat'
with open(filename, 'rb') as f:
content = f.read()
print(binascii.hexlify(content))

