社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python to_bytes函数将一个内存位置转换为十六进制,但将另一个内存位置转换为字符[duplicate]

Grazosi • 3 年前 • 1495 次点击  
>>> struct.pack('2I',12, 30)
b'\x0c\x00\x00\x00\x1e\x00\x00\x00'    
>>> struct.pack('2I',12, 31)
b'\x0c\x00\x00\x00\x1f\x00\x00\x00'
>>> struct.pack('2I',12, 32)
b'\x0c\x00\x00\x00 \x00\x00\x00'
                  ^in question
>>> struct.pack('2I',12, 33)
b'\x0c\x00\x00\x00!\x00\x00\x00'
                  ^in question

我希望所有值都显示为十六进制

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/128545
 
1495 次点击  
文章 [ 5 ]  |  最新文章 3 年前
Artem Zankovich
Reply   •   1 楼
Artem Zankovich    8 年前

有一个选项可以将字节数组转换为十六进制字符串 encode 。它适用于Python 2.4中的任何Python:

Python 2.7.12 (default, Oct 10 2016, 12:50:22)
>>> import struct
>>> struct.pack('2I',12, 30).encode('hex')
'0c0000001e000000'
>>> struct.pack('2I',12, 31).encode('hex')
'0c0000001f000000'
>>> struct.pack('2I',12, 32).encode('hex')
'0c00000020000000'
>>> struct.pack('2I',12, 33).encode('hex')
'0c00000021000000'
apaderno
Reply   •   2 楼
apaderno    5 年前

在Python 3.7字节中,对象没有 encode() 方法下面的代码不再有效。

import struct

hex_str = struct.pack('2I',12, 30).encode('hex')

而不是 编码() ,Python 3.7代码应使用 hex() 方法,在Python 3.5中介绍。

import struct

# hex_str will contain the '0c0000001e000000' string.
hex_str = struct.pack('2I',12, 30).hex()
Tim Peters
Reply   •   3 楼
Tim Peters    11 年前

如果你想的话,你必须自己重新格式化 \x 到处逃走;例如。,

>>> import struct
>>> r = struct.pack('2I',12, 33)
>>> r
b'\x0c\x00\x00\x00!\x00\x00\x00'
>>> list(r)
[12, 0, 0, 0, 33, 0, 0, 0]
>>> print("".join("\\x%02x" % i for i in r))
\x0c\x00\x00\x00\x21\x00\x00\x00
Lukas Graf
Reply   •   4 楼
Lukas Graf    11 年前

他的呢?

>>> data = struct.pack('2I',12, 30)
>>> [hex(ord(c)) for c in data]
['0xc', '0x0', '0x0', '0x0', '0x1e', '0x0', '0x0', '0x0']

表情 [item for item in sequence] 这就是所谓的 list comprehension .这基本上是一种非常简洁的写作方法 for 循环,并根据结果创建列表。

这个 ord() 内置函数接受一个字符串,并将其转换为一个整数,该整数是其对应的unicode代码点(对于ASCII字符集中的字符,其值与ASCII表中的值相同)。

它的对应物, chr() 8比特字符串或 unichr() 对于unicode对象,则相反。

这个 hex() 然后,builtin简单地将整数转换为十六进制表示形式。


正如@TimPeters所指出的,在Python 3中,您需要丢失 ord() ,因为迭代字节对象将(已经)产生整数:

Python 3.4.0a3 (default, Nov  8 2013, 18:33:56)
>>> import struct
>>> data = struct.pack('2I',12, 30)
>>> type(data)
<class 'bytes'>
>>> type(data[1])
<class 'int'>
>>>
>>> [hex(i) for i in data]
['0xc', '0x0', '0x0', '0x0', '0x1e', '0x0', '0x0', '0x0']
Mark Tolonen
Reply   •   5 楼
Mark Tolonen    4 年前

看到了吗 bytes.hex() :

>>> import struct
>>> struct.pack('2I',12,30).hex()   # available since Python 3.5
'0c0000001e000000'
>>> struct.pack('2I',12,30).hex(' ')  # separator available since Python 3.8
'0c 00 00 00 1e 00 00 00'
>>> struct.pack('2I',12,30).hex(' ',4) # bytes_per_sep also since Python 3.8
'0c000000 1e000000'

较老的Python使用 binascii.hexlify :

>>> import binascii
>>> import struct
>>> binascii.hexlify(struct.pack('2I',12,30))
b'0c0000001e000000'

或者,如果希望使用空格使其更具可读性:

>>> ' '.join(format(n,'02X') for n in struct.pack('2I',12,33))
'0C 00 00 00 21 00 00 00'

Python 3.6+,使用f字符串(但是 .hex() 是可用且更容易的)。

>>> ' '.join(f'{n:02X}' for n in struct.pack('2I',12,33))
'0C 00 00 00 21 00 00 00'