在Python

时间:2017-09-08 06:14:55

标签: python python-3.x

我有一个带十六进制字符的长字符串 例如:

string = "AA55CC3301AA55CC330F234567"

我正在使用

string.to_bytes(4, 'little')

我希望最后一个字符串如下:

6745230F33CC55AA0133CC55AA

但是我收到了错误

  

AttributeError: 'str' object has no attribute 'to_bytes'

这里有什么问题?

5 个答案:

答案 0 :(得分:1)

要在小端和大端之间转换,可以使用基于ComposedSchemaallOf的{​​{1}}函数:

convert_hex

答案 1 :(得分:0)

也许你可以反转字符串之一

* * * * * root  /root/vasanth/lsef/script.sh

答案 2 :(得分:0)

这是您要找的答案/代码吗?

def little(string):
 t= bytearray.fromhex(string)
 t.reverse()
 return ''.join(format(x,'02x') for x in t).upper()
little(s=AA55CC3301AA55CC330F234567)

答案 3 :(得分:0)

issue仅适用于整数,afaik。

您可以使用to_bytes

react-google-maps side

使用bytearray将其转换回字符串:

>>> ba = bytearray.fromhex("AA55CC3301AA55CC330F234567")
>>> ba.reverse()

答案 4 :(得分:0)

请注意,您的问题与this question from 2009非常相似。尽管旧线程要求单向转换,而您要求“反之亦然”转换,但无论您以哪种字节序开头,它实际上都是同一回事。让我展示一下,

0x12345678 -> 0x78563412 -> 0x12345678

使用pwntools(为软件黑客开发的工具)进行转换非常容易。特别是为了避免打包乱堆,pwntools embeds p32() function for exact this purpose

import pwntools

x2 = p32(x1)
相关问题