简体   繁体   English

在python中将二进制数据正确转换为十六进制

[英]Converting binary data to hex in python properly

I'm working on a program that uses a BMP and a separate file for the transparency layer. 我正在开发一个使用BMP和一个单独的文件作为透明层的程序。 I need to convert them into a PNG from that so I'm using PIL in python to do so. 我需要从中将它们转换为PNG,所以我在python中使用PIL来做到这一点。 However, I need the data from the transparency file in hex so it can be added to the image. 但是,我需要十六进制透明文件中的数据,以便可以将其添加到图像中。 I am using the binascii.hexlify function to do that. 我正在使用binascii.hexlify函数来做到这一点。

Now, the problem I'm having is for some reason the data, after going through the hexlify function (I've systematically narrowed it down by going through my code piece by piece), looks different than it does in my hex editor and is causing slight distortions in the images. 现在,由于某种原因,我遇到的问题是数据在通过hexlify函数后(通过逐段检查代码系统地缩小了范围),看起来与在hex编辑器中不同。导致图像轻微变形。 I can't seem to figure out where I am going wrong. 我似乎无法弄清楚哪里出了问题。

Data before processing in Hex editor 在十六进制编辑器中处理之前的数据

Data after processing in Hex editor 在十六进制编辑器中处理后的数据

Here is the problematic part off my code: 这是我的代码中有问题的部分:

filename = askopenfilename(parent=root)
with open(filename, 'rb') as f:
    content = f.read()
    f.close()
hexContent = binascii.hexlify(content).decode("utf-8")

My input 我的输入

My output (This is hexcontent written to a file. Since I know that it is not going wrong in the writing of the file, and it is also irrelevant to my actual program I did not add that part to the code snippet) 我的输出 (这是写入文件的hexcontent。由于我知道在写入文件时不会出错,并且与我的实际程序无关,所以我没有将该部分添加到代码段中)

Before anyone asks I tried codecs.encode(content, 'hex') and binascii.b2a_hex(content). 在有人问之前,我尝试了codecs.encode(content,'hex')和binascii.b2a_hex(content)。

As for how I know that it is this part that is messing up, I printed out binascii.hexlify(content) and found the same part as in the hex editor and it looked identical to what I had got in the end. 至于我怎么知道这是一个混乱的部分,我打印出binascii.hexlify(content)并找到了与十六进制编辑器相同的部分,它看起来与我最后得到的完全相同。

Another possibility for where it is going wrong is in the "open(filename, 'rb')" step. 出现问题的另一种可能性是在“ open(filename,'rb')”步骤中。 I haven't yet thought of a way to test that. 我还没有想到一种测试方法。 So any help or suggestions would be appreciated. 因此,任何帮助或建议将不胜感激。 If you need one of the files I'm using for testing purposes, I'll gladly add one here. 如果您需要我用于测试的文件之一,我很乐意在此处添加一个。

If I understand your question correctly then your desired output should match Data before processing in Hex editor . 如果我正确理解了您的问题,则在使用Hex编辑器处理之前,所需的输出应与Data相匹配。 I can obtain this with the following code: 我可以通过以下代码获得它:

with open('Input.alp', 'rb') as f:
    i = 0
    for i, chunk in enumerate(iter(lambda: f.read(16), b'')):
        if 688 <= i * 16 <= 736:
            print i * 16, chunk.encode('hex')

Outputs: 输出:

688 ffffffffffffffffffffffffffffffff
704 ffffffffffffffffffffffe000000000
720 000000000000000001dfffffffffffff
736 ffffffffffffffffffffffffffffffff

See this answer for a more detailed explanation. 有关更多详细说明,请参见此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM