Python:将文件中的十六进制读入列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35516183/
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-19 16:32:37  来源:igfitidea点击:

Python: Read hex from file into list?

pythonhex

提问by Joseph A.

Is there a simple way to, in Python, read a file's hexadecimal data into a list, say hex?

有没有一种简单的方法可以在 Python 中将文件的十六进制数据读入列表,比如说hex

So hexwould be this:

所以hex会是这样:

hex = ['AA','CD','FF','0F']

hex = ['AA','CD','FF','0F']

I don't want to have to read into a string, then split. This is memory intensive for large files.

我不想读入一个字符串,然后拆分。这对于大文件来说是内存密集型的。

采纳答案by OneCricketeer

s = "Hello"
hex_list = ["{:02x}".format(ord(c)) for c in s]

Output

输出

['48', '65', '6c', '6c', '6f']

Just change sto open(filename).read()and you should be good.

只需更改sopen(filename).read(),您应该会很好。

with open('/path/to/some/file', 'r') as fp:
    hex_list = ["{:02x}".format(ord(c)) for c in fp.read()]


Or, if you do not want to keep the whole list in memory at once for large files.

或者,如果您不想将整个列表一次保存在内存中以保存大文件。

hex_list = ("{:02x}".format(ord(c)) for c in fp.read())

and to get the values, keep calling

并获取值,继续调用

next(hex_list)

to get all the remaining values from the generator

从生成器中获取所有剩余的值

list(hex_list)

回答by aghast

Be aware that for viewing hexadecimal dumps of files, there are utilities available on most operating systems. If all you want to do is hex dump the file, consider one of these programs:

请注意,为了查看文件的十六进制转储,大多数操作系统上都有可用的实用程序。如果您只想对文件进行十六进制转储,请考虑以下程序之一:

  • od(octal dump, which has a -xor -t xoption)
  • hexdump
  • xdutility available under windows
  • Online hex dump tools, such as this one.
  • od(八进制转储,有一个-x-t x选项)
  • hexdump
  • xd在 windows 下可用的实用程序
  • 在线十六进制转储工具,例如这个

回答by Pynchia

Using Python 3, let's assume the input file contains the sample bytes you show. For example, we can create it like this

使用 Python 3,假设输入文件包含您显示的示例字节。例如,我们可以像这样创建它

>>> inp = bytes((170,12*16+13,255,15)) # i.e. b'\xaa\xcd\xff\x0f'
>>> with open(filename,'wb') as f:
...     f.write(inp)

Now, given we want the hex representation of each byte in the input file, it would be nice to open the file in binary mode, without trying to interpret its contents as characters/strings (or we might trip on the error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte)

现在,假设我们想要输入文件中每个字节的十六进制表示,最好以二进制模式打开文件,而不必尝试将其内容解释为字符/字符串(否则我们可能会出错UnicodeDecodeError: 'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte

>>> with open(filename,'rb') as f:
...     buff = f.read() # it reads the whole file into memory
...
>>> buff
b'\xaa\xcd\xff\x0f'
>>> out_hex = ['{:02X}'.format(b) for b in buff]
>>> out_hex
['AA', 'CD', 'FF', '0F']

If the file is large, we might want to read one character at a time or in chunks. For that purpose I recommend to read this Q&A

如果文件很大,我们可能希望一次或分块读取一个字符。为此,我建议阅读此问答