python-3.x - 读取PE文件时如何将字节转换为手动输出

标签 python-3.x byte decode

我的代码:

# -*- coding: utf-8 -*- #

if __name__ == "__main__":
    try:
        f = open("test.dll", 'rb')
        context = f.read()
        f.close()
        print(context)
    except Exception as e:
        print(e)

当我使用 python3 时,输出是这样的字节:

b'b\'MZ\x90\x00\x03\x00\x00\x00\x04\x00\x00\x00\xff\xff\x00\x00\xb8\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0\x00\x00\x00\x0e\x1f\xba\x0e\x00\xb4\t\xcd!\xb8\x01L\xcd!This program cannot be run in DOS mode.\r\r\n$\x00\x00\x00\x00



当我使用python2时,我想要的输出是这样的:

MZ?  ? @ ? ? ???L?This program cannot be run in DOS mode.

$ 2!跪v@住v@住v@住8B@住8^@住8T‰@住8D



如何使用 python3 获得类似 python2 的输出?

PS:
我尝试像这样解码输出:
context = f.read().decode("utf-8")

但是有这样的错误:

'utf-8' codec can't decode byte 0x90 in position 2: invalid start byte



另一种解码类型具有相同的错误。

我怎样才能得到正确的输出?多谢。

最佳答案

哦,对不起
我有方法:

代码:

# -*- coding: utf-8 -*- #

if __name__ == "__main__":
     try:
        f = open("test.dll", 'rb')
        context = f.read().decode("utf-8", 'ignore')
        f.close()
        print(context)
    except Exception as e:
        print(e)

使用“忽略”参数

真相在这里:enter link description here

关于python-3.x - 读取PE文件时如何将字节转换为手动输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46115992/

相关文章:

python - 如何从深度差异中完全排除 "type_changes",因为我只关心值的变化?

python - 在 matplotlib 图上嵌入小 map (cartopy)

c - 为什么要添加填充,如果 char 在 int 之后?

文本文件的字节表示

iphone - 解码引用打印品

performance - ffmpeg 在没有 avformat_find_stream_info 的情况下解码慢速调用

python-3.x - 凭证文件的 boto3 格式和位置

java - 字节文字上的编译器错误 "Incompatible types"

mysql - 从数据库返回解码后的文本

python - url 无法处理代码,但可以手动搜索