python)将大恩迪安转换为小恩迪安

发布于 2025-02-06 05:35:40 字数 576 浏览 2 评论 0原文

我有一个文件(.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

白龙吟 2025-02-13 05:35:40

Using a bytearray (Try it online!):

data = "F324658951425AF3EB0011"
bits = [16, 8, 8, 32, 8, 16]

b = bytearray.fromhex(data)
i = 0
for n in bits:
    n //= 8
    b[i:i+n] = reversed(b[i:i+n])
    i += n
print(b.hex().upper())

Or with memoryview (

data = "F324658951425AF3EB0011"
bits = [16, 8, 8, 32, 8, 16]

b = bytearray.fromhex(data)
m = memoryview(b)
for n in bits:
    n //= 8
    m[:n] = m[n-1::-1]
    m = m[n:]
print(b.hex().upper())

Using a bytearray (Try it online!):

data = "F324658951425AF3EB0011"
bits = [16, 8, 8, 32, 8, 16]

b = bytearray.fromhex(data)
i = 0
for n in bits:
    n //= 8
    b[i:i+n] = reversed(b[i:i+n])
    i += n
print(b.hex().upper())

Or with memoryview (Try it online!):

data = "F324658951425AF3EB0011"
bits = [16, 8, 8, 32, 8, 16]

b = bytearray.fromhex(data)
m = memoryview(b)
for n in bits:
    n //= 8
    m[:n] = m[n-1::-1]
    m = m[n:]
print(b.hex().upper())
甜嗑 2025-02-13 05:35:40

这是一种简单但有效的方法:

hs = 'F324658951425AF3EB0011'
m = [16, 8, 8, 32, 8, 16]
idx = 0
result = ''
for w in m:
    match w:
        case 8:
            result += hs[idx:idx+2]
        case 16:
            result += hs[idx+2:idx+4] + hs[idx:idx+2]
        case 32:
            result += hs[idx+6:idx+8] + hs[idx+4:idx+6] + hs[idx+2:idx+4] + hs[idx:idx+2]
    idx += w // 4
print(result)

输出:

24F36589F35A4251EB1100

Here's a simplistic but effective approach:

hs = 'F324658951425AF3EB0011'
m = [16, 8, 8, 32, 8, 16]
idx = 0
result = ''
for w in m:
    match w:
        case 8:
            result += hs[idx:idx+2]
        case 16:
            result += hs[idx+2:idx+4] + hs[idx:idx+2]
        case 32:
            result += hs[idx+6:idx+8] + hs[idx+4:idx+6] + hs[idx+2:idx+4] + hs[idx:idx+2]
    idx += w // 4
print(result)

Output:

24F36589F35A4251EB1100
我的影子我的梦 2025-02-13 05:35:40

在广泛的笔触中,

>>> data = "F324658951425AF3EB0011"
>>> bits = [16, 8, 8, 32, 8, 16]

我首先要获得单个字节大小的(即两个十六进制字符)的作品:

>>> bs = [data[i:i+2] for i in range(0, len(data), 2)] # get individual bytes
>>> bs
['F3', '24', '65', '89', '51', '42', '5A', 'F3', 'EB', '00', '11']

然后,我会使用迭代器根据位列表中的值(每8位字节)中的值来获取许多字节(每8位字节) ,然后按反向顺序添加这片字符串。然后在最后加入:

>>> iterator = iter(bs)
>>> result = []
>>> for n in bits:
...     chunk = [next(iterator) for _ in range(n//8)]
...     result.extend(reversed(chunk))
...
>>> print("".join(result))
24F36589F35A4251EB1100

In broad strokes,

>>> data = "F324658951425AF3EB0011"
>>> bits = [16, 8, 8, 32, 8, 16]

I would first just get the individual byte-sized (i.e. two hex characters) pieces:

>>> bs = [data[i:i+2] for i in range(0, len(data), 2)] # get individual bytes
>>> bs
['F3', '24', '65', '89', '51', '42', '5A', 'F3', 'EB', '00', '11']

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:

>>> iterator = iter(bs)
>>> result = []
>>> for n in bits:
...     chunk = [next(iterator) for _ in range(n//8)]
...     result.extend(reversed(chunk))
...
>>> print("".join(result))
24F36589F35A4251EB1100
牵强ㄟ 2025-02-13 05:35:40

我可能会循环thorugh您的规则列表(您的[16、8、8、32、8、16])。对于规则中的每个元素,请从.vcon 中获取规则[i]/8 touples,然后以相反的顺序添加到>结果

像这样:

rule = [16, 8, 8, 32, 8, 16] 
bigE = 'F324658951425AF3EB0011'
littleE = ''

for x in rule:
    n = x//4
    word, bigE = bigE[:n], bigE[n:]
    for i in range(x//8):        
        word, byte = word[:-2], word[-2:]
        littleE += byte
        
print(littleE)

I would probably loop thorugh your rule list (your [16, 8, 8, 32, 8, 16]). For each element in rule, take rule[i]/8 touples from .vcon and add these touples in reverse order to your result.

Like so:

rule = [16, 8, 8, 32, 8, 16] 
bigE = 'F324658951425AF3EB0011'
littleE = ''

for x in rule:
    n = x//4
    word, bigE = bigE[:n], bigE[n:]
    for i in range(x//8):        
        word, byte = word[:-2], word[-2:]
        littleE += byte
        
print(littleE)
無心 2025-02-13 05:35:40

[16、8、8、32、8、16]数组将业务放在一边 - 您可以使用切片将十六进制字符串从大型Endian转换为Little Endian(或反之亦然)。首先,将十六进制字符串转换为字节,然后使用[::- 1]逆转字节的顺序,然后将字节转换回十六进制,如下:

bigendian='f324658951425af3eb0011'
littleendian=bytes.fromhex(bigendian)[::-1].hex()
print('bigendian:', bigendian)
print('littleendian:', littleendian)

这会产生:

bigendian: f324658951425af3eb0011
littleendian: 1100ebf35a4251896524f3 

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:

bigendian='f324658951425af3eb0011'
littleendian=bytes.fromhex(bigendian)[::-1].hex()
print('bigendian:', bigendian)
print('littleendian:', littleendian)

This produces:

bigendian: f324658951425af3eb0011
littleendian: 1100ebf35a4251896524f3 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文