python)将大恩迪安转换为小恩迪安
我有一个文件(.VCON),其中包含存储在Big Endian中的十六进制字符串(约2,000个字节),并希望根据规则集将此文件转换为Little Endian HexadeCimal String .VCON .VCON文件。
在列表中,有四个可能的值:8、16、32、64 如果列表中的数字为8,则不需要switiching(从大到小恩迪安),因为数据是一个字节。 除8以外,必须将数据从大型末日切换到小末日。
我很难想到解决这个问题。
例如,如果我的数据在.vcon文件(big endian)中如下
F324658951425AF3EB0011
,并且列表中的数字如下
[16, 8, 8, 32, 8, 16]
,那么我们创建的结果数据应如下(对Little Endian),
24F36589F35A4251EB1100
我应该如何遍历中的数字同时还访问十六进制字符串文件中的每个字节(以大型endian格式)访问每个字节,并以Little Endian格式创建一个新的十六进制字符串文件?
I have a file (.vcon) that contains hexadecimal strings (around 2,000 bytes) stored in big endian and want to convert this file to little endian hexadecimal string .vcon file based on the rule set.
Inside a list, there are four possible values : 8, 16, 32, 64
If a number in a list is 8, then no switiching (from big to little endian) is necessary since the data is one byte.
Other than 8, the data must be switched from big to little endian.
I have trouble coming up with a way to go about this.
For example, if my data in the .vcon file (big endian) is as follows
F324658951425AF3EB0011
and the numbers in the list are as follows
[16, 8, 8, 32, 8, 16]
then the resulting data we create should be as follows (to little endian)
24F36589F35A4251EB1100
How should I iterate through the numbers in a list while also accessing each byte in a hexadecimal string file (that is in big endian format) and create a new hexadecimal string file in little endian format?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Using a bytearray (Try it online!):
Or with memoryview (
Using a bytearray (Try it online!):
Or with memoryview (Try it online!):
这是一种简单但有效的方法:
输出:
Here's a simplistic but effective approach:
Output:
在广泛的笔触中,
我首先要获得单个字节大小的(即两个十六进制字符)的作品:
然后,我会使用迭代器根据位列表中的值(每8位字节)中的值来获取许多字节(每8位字节) ,然后按反向顺序添加这片字符串。然后在最后加入:
In broad strokes,
I would first just get the individual byte-sized (i.e. two hex characters) pieces:
Then, I would use an iterator to take a number of bytes based on the value in the bits list (a byte per 8 bits), then add this chunk of strings to a list in reversed order. Then join at the end:
我可能会循环thorugh您的.vcon 中获取
规则
列表(您的[16、8、8、32、8、16])。对于规则
中的每个元素,请从规则[i]/8
touples,然后以相反的顺序添加到>结果
。像这样:
I would probably loop thorugh your
rule
list (your [16, 8, 8, 32, 8, 16]). For each element inrule
, takerule[i]/8
touples from.vcon
and add these touples in reverse order to yourresult
.Like so:
用
[16、8、8、32、8、16]
数组将业务放在一边 - 您可以使用切片将十六进制字符串从大型Endian转换为Little Endian(或反之亦然)。首先,将十六进制字符串转换为字节,然后使用[::- 1]
逆转字节的顺序,然后将字节转换回十六进制,如下:这会产生:
Putting aside the business with the
[16, 8, 8, 32, 8, 16]
array - you can use slicing to convert a hex string from big endian to little endian (or vice-versa). First, convert the hex string to bytes, then use[::-1]
to reverse the order of the bytes, then convert the bytes back to hex, like so:This produces: