python - 从文件中读取小端并转换为十进制

标签 python python-2.7

我有一个包含无符号 64 位整数的文件,格式如下所示

0100 0000 0000 0000 
0200 0000 0000 0000 
0300 0000 0000 0000 
3655 9d80 0f00 0000 
7a64 dae3 0900 0000 
060f fa3f 0600 0000

我正在寻找一种方法来读取这些数字,然后将它们转换为十进制等值

我目前的代码如下:

filename = "C:\\RainbowTables\\md5_loweralpha-numeric#1-7_0_2x50_0.rt"
blocksize = 8

with open(filename, "rb") as f:
    startpoint = f.read(blocksize)
    string = ""
    for ch in startpoint:
        string += hex(ord(ch))
    print string

这给了我第一个数字的以下输出

0x10x00x00x00x00x00x00x0

我一直在研究如何为此使用结构,因为看起来它就是为此而设计的,但我一直无法找到使用它们的正确语法。

如有任何帮助,我们将不胜感激!

最佳答案

import struct
decoded = []
with open("some_bin_file.rt","rb") as f:
     while True:
          try: 
             decoded.append(struct.unpack_from("<Q",f)[0])
             # `<` means little endian; `Q` means unsigned long long (8 bytes)            
          except struct.error:
             break

print decoded

我认为应该可以...

关于python - 从文件中读取小端并转换为十进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24965862/

相关文章:

python - python编码检测库

python - Celery 任务结果不会与 rpc 保持一致

python - 在Python中 reshape 数据框

python - 在ndarray python中获取第一维

Python:展平对象列表

python - many2many 字段上的 Odoo 特定表单 View

python - 如何在 Python 3.3 中为所有键(在列表中给出)用 None 填充字典

python - 通过主键传递列表作为参数从任意表加载记录

Python PYDOBC 使用参数插入 SQL Server DB

multithreading - 在 python 中使用 get_nowait() 而不引发空异常