使用 python 将字节反向 AB CD 转换为 CD AB

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

byte reverse AB CD to CD AB with python

pythonbytereversing

提问by james28909

I have a .bin file, and I want to simply byte reverse the hex data. Say for instance @ 0x10 it reads AD DE DE C0, want it to read DE AD C0 DE.

我有一个 .bin 文件,我想简单地对十六进制数据进行字节反转。比如说@0x10它读AD DE DE C0,要它读DE AD C0 DE。

I know there is a simple way to do this, but I am am beginner and just learning python and am trying to make a few simple programs to help me through my daily tasks. I would like to convert the whole file this way, not just 0x10.

我知道有一种简单的方法可以做到这一点,但我是初学者,刚刚学习 Python,我正在尝试制作一些简单的程序来帮助我完成日常任务。我想以这种方式转换整个文件,而不仅仅是 0x10。

I will be converting at start offset 0x000000 and blocksize/length is 1000000.

我将在起始偏移量 0x000000 处进行转换,块大小/长度为 1000000。

here is my code, maybe you can tell me what to do. i am sure i am just not getting it, and i am new to programming and python. if you could help me i would very much appreciate it.

这是我的代码,也许你可以告诉我该怎么做。我确定我只是没有得到它,而且我是编程和 python 的新手。如果你能帮助我,我将不胜感激。

def main():
    infile = open("file.bin", "rb")
    new_pos = int("0x000000", 16)
    chunk = int("1000000", 16)
    data = infile.read(chunk)
    reverse(data)

def reverse(data):
    output(data)

def output(data):
    with open("reversed", "wb") as outfile:
        outfile.write(data)

main()

and you can see the module for reversing, i have tried many different suggestions and it will either pass the file through untouched, or it will throw errors. i know module reverse is empty now, but i have tried all kinds of things. i just need module reverse to convert AB CD to CD AB. thanks for any input

并且您可以看到用于反转的模块,我尝试了许多不同的建议,它要么原封不动地传递文件,要么抛出错误。我知道模块 reverse 现在是空的,但我已经尝试了各种方法。我只需要反向模块即可将 AB CD 转换为 CD AB。感谢您提供任何意见

EDIT: the file is 16 MB and i want to reverse the byte order of the whole file.

编辑:该文件是 16 MB,我想反转整个文件的字节顺序。

采纳答案by Raymond Hettinger

In Python 2, the binary file gets read as a string, so string slicing should easily handle the swapping of adjacent bytes:

在 Python 2 中,二进制文件作为字符串读取,因此字符串切片应该可以轻松处理相邻字节的交换:

>>> original = '\xAD\xDE\xDE\xC0'
>>> ''.join([c for t in zip(original[1::2], original[::2]) for c in t])
'\xde\xad\xc0\xde'

In Python 3, the binary file gets read as bytes. Only a small modification is need to build another array of bytes:

在 Python 3 中,二进制文件被读取为字节。只需要一个小的修改来构建另一个字节数组:

>>> original = b'\xAD\xDE\xDE\xC0'
>>> bytes([c for t in zip(original[1::2], original[::2]) for c in t])
b'\xde\xad\xc0\xde'

You could also use the <and >endianessformat codes in the struct moduleto achieve the same result:

您还可以在struct 模块中使用<>endianess格式代码来实现相同的结果:

>>> struct.pack('<2h', *struct.unpack('>2h', original))
'\xde\xad\xc0\xde'

Happy byte swapping:-)

快乐字节交换:-)

回答by Tim Morgan

Python has a list operator to reverse the values of a list --> nameOfList[::-1]

Python 有一个列表运算符来反转列表的值 --> nameOfList[::-1]

So, I might store the hex values as string and put them into a list then try something like:

因此,我可能会将十六进制值存储为字符串并将它们放入列表中,然后尝试以下操作:

def reverseList(aList):
rev = aList[::-1]
outString = ""
for el in rev:
    outString += el + " "
return outString

回答by grawee

In Python 3.4 you can use this:

在 Python 3.4 中你可以使用这个:

>>> data = b'\xAD\xDE\xDE\xC0'
>>> swap_data = bytearray(data)
>>> swap_data.reverse()

the result is

结果是

bytearray(b'\xc0\xde\xde\xad')

回答by u4751247

data = b'\xAD\xDE\xDE\xC0'
reversed_data = data[::-1]
print(reversed_data)
# b'\xc0\xde\xde\xad'