将整数转换为特定格式的十六进制字符串

发布于 2024-11-27 03:32:16 字数 450 浏览 0 评论 0原文

我是 python 新手,有以下问题:我需要将整数转换为 6 个字节的十六进制字符串。

例如 281473900746245 --> "\xFF\xFF\xBF\xDE\x16\x05"

十六进制字符串的格式很重要。 int 值的长度是可变的。

格式“0xffffbf949309L”对我不起作用。 (我用 hex(int-value) 得到这个)


我的最终解决方案(经过一些“玩”)是:

def _tohex(self, int_value):
    data_ = format(int_value, 'x')

    result = data_.rjust(12, '0')
    hexed = unhexlify(result)

    return hexed

感谢您的所有帮助!

I am new to python and have following problem: I need to convert an integer to a hex string with 6 bytes.

e.g.
281473900746245 --> "\xFF\xFF\xBF\xDE\x16\x05"

The format of the hex-string is important. The length of the int value is variable.

The format '0xffffbf949309L' don't work for me. (I get this with hex(int-value))


My final solution (after some "playing") is:

def _tohex(self, int_value):
    data_ = format(int_value, 'x')

    result = data_.rjust(12, '0')
    hexed = unhexlify(result)

    return hexed

Thank you for all the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

疧_╮線 2024-12-04 03:32:16

可能有更好的解决方案,但您可以这样做:

x = 281473900746245
decoded_x = hex(x)[2:].decode('hex') # value: '\xff\xff\xbf\xde\x16\x05'

细分:

hex(x)                     # value: '0xffffbfde1605'
hex(x)[2:]                 # value: 'ffffbfde1605'
hex(x)[2:].decode('hex')   # value: '\xff\xff\xbf\xde\x16\x05'

更新:

根据@multipleinstances和@Sven的评论,由于您可能正在处理长值,因此您可能需要调整十六进制的输出一点点:

format(x, 'x')     # value: 'ffffbfde1605'

但是,有时,十六进制的输出可能是奇数长度,这会破坏解码,因此最好创建一个函数来执行此操作:

def convert(int_value):
   encoded = format(int_value, 'x')

   length = len(encoded)
   encoded = encoded.zfill(length+length%2)

   return encoded.decode('hex')

There might be a better solution, but you can do this:

x = 281473900746245
decoded_x = hex(x)[2:].decode('hex') # value: '\xff\xff\xbf\xde\x16\x05'

Breakdown:

hex(x)                     # value: '0xffffbfde1605'
hex(x)[2:]                 # value: 'ffffbfde1605'
hex(x)[2:].decode('hex')   # value: '\xff\xff\xbf\xde\x16\x05'

Update:

Per @multipleinstances and @Sven's comments, since you might be dealing with long values, you might have to tweak the output of hex a little bit:

format(x, 'x')     # value: 'ffffbfde1605'

Sometimes, however, the output of hex might be an odd-length, which would break decode, so it'd probably be better to create a function to do this:

def convert(int_value):
   encoded = format(int_value, 'x')

   length = len(encoded)
   encoded = encoded.zfill(length+length%2)

   return encoded.decode('hex')
笑咖 2024-12-04 03:32:16

在 Python 3.2 或更高版本中,您可以使用 to_bytes()< /code>整数方法。

>>> i = 281473900746245       
>>> i.to_bytes((i.bit_length() + 7) // 8, "big")
b'\xff\xff\xbf\xde\x16\x05'

In Python 3.2 or above, you can use the to_bytes() method of the interger.

>>> i = 281473900746245       
>>> i.to_bytes((i.bit_length() + 7) // 8, "big")
b'\xff\xff\xbf\xde\x16\x05'
小…楫夜泊 2024-12-04 03:32:16

如果您不使用 Python 3.2(我很确定您不使用),请考虑下一个方法:

>>> i = 281473900746245
>>> hex_repr = []
>>> while i:
...     hex_repr.append(struct.pack('B', i & 255))
...     i >>= 8
...
>>> ''.join(reversed(hex_repr))
'\xff\xff\xbf\xde\x16\x05'

If you don't use Python 3.2 (I'm pretty sure you don't), consider the next approach:

>>> i = 281473900746245
>>> hex_repr = []
>>> while i:
...     hex_repr.append(struct.pack('B', i & 255))
...     i >>= 8
...
>>> ''.join(reversed(hex_repr))
'\xff\xff\xbf\xde\x16\x05'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文