Python:二进制/十六进制字符串转换?

发布于 2024-07-30 11:46:57 字数 350 浏览 1 评论 0原文

我有一个既有二进制字符又有字符串字符的字符串,我想先将其转换为二进制,然后再转换为十六进制。

该字符串如下:

<81>^Q<81>"^Q^@^[)^G ^Q^A^S^A^V^@<83>^Cd<80><99>}^@N^@^@^A^@^@^@^@^@^@^@j

如何在 Python 中转换该字符串,以便十六进制格式的输出类似于下面的内容?

24208040901811001B12050809081223431235113245422F0A23000000000000000000001F

I have a string that has both binary and string characters and I would like to convert it to binary first, then to hex.

The string is as below:

<81>^Q<81>"^Q^@^[)^G ^Q^A^S^A^V^@<83>^Cd<80><99>}^@N^@^@^A^@^@^@^@^@^@^@j

How do I go about converting this string in Python so that the output in hex format is similar to this below?

24208040901811001B12050809081223431235113245422F0A23000000000000000000001F

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

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

发布评论

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

评论(3

北风几吹夏 2024-08-06 11:46:57

您可以像这样使用 ord 和 hex :

>>> s = 'some string'
>>> hex_chars = map(hex,map(ord,s))
>>> print hex_chars
['0x73', '0x6f', '0x6d', '0x65', '0x20', '0x73', '0x74', '0x72', '0x69', '0x6e', '0x67']
>>> hex_string = "".join(c[2:4] for c in hex_chars)
>>> print hex_string
736f6d6520737472696e67
>>>

或者使用内置编码:

>>> s = 'some string'
>>> print s.encode('hex_codec')
736f6d6520737472696e67
>>>

You can use ord and hex like this :

>>> s = 'some string'
>>> hex_chars = map(hex,map(ord,s))
>>> print hex_chars
['0x73', '0x6f', '0x6d', '0x65', '0x20', '0x73', '0x74', '0x72', '0x69', '0x6e', '0x67']
>>> hex_string = "".join(c[2:4] for c in hex_chars)
>>> print hex_string
736f6d6520737472696e67
>>>

Or use the builtin encoding :

>>> s = 'some string'
>>> print s.encode('hex_codec')
736f6d6520737472696e67
>>>
瀟灑尐姊 2024-08-06 11:46:57
>>> import binascii

>>> s = '2F'

>>> hex_str = binascii.b2a_hex(s)

>>> hex_str

>>> '3246'

或者

>>>import binascii

>>> hex_str = binascii.hexlify(s)

>>> hex_str
>>> '3246'
>>>
>>> import binascii

>>> s = '2F'

>>> hex_str = binascii.b2a_hex(s)

>>> hex_str

>>> '3246'

OR

>>>import binascii

>>> hex_str = binascii.hexlify(s)

>>> hex_str
>>> '3246'
>>>
雨巷深深 2024-08-06 11:46:57

更快的解决方案请参阅:

from timeit import Timer

import os
import binascii

def testSpeed(statement, setup = 'pass'):
  print '%s' % statement
  print '%s' % Timer(statement, setup).timeit()

setup = """
import os

value = os.urandom(32)
"""

# winner
statement = """
import binascii

binascii.hexlify(value)
"""

testSpeed(statement, setup)

# loser
statement = """
import binascii

value.encode('hex_codec')
"""

testSpeed(statement, setup)

结果:

import binascii

binascii.hexlify(value)

2.18547999816

value.encode('hex_codec')

2.91231595077

Faster solution see:

from timeit import Timer

import os
import binascii

def testSpeed(statement, setup = 'pass'):
  print '%s' % statement
  print '%s' % Timer(statement, setup).timeit()

setup = """
import os

value = os.urandom(32)
"""

# winner
statement = """
import binascii

binascii.hexlify(value)
"""

testSpeed(statement, setup)

# loser
statement = """
import binascii

value.encode('hex_codec')
"""

testSpeed(statement, setup)

Results:

import binascii

binascii.hexlify(value)

2.18547999816

value.encode('hex_codec')

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