在Python中,输出字节数组可以通过以下几种方法:
1. 使用`print()`函数直接打印字节数组。
byte_array = bytes('Hello, World!', 'utf-8')
print(byte_array)
输出结果:
b'Hello, World!'
2. 使用`hex()`函数将字节数组转换为十六进制字符串表示。
byte_array = bytes('Hello, World!', 'utf-8')
hex_str = ' '.join([format(x, '02x') for x in byte_array])
print(hex_str)
输出结果:
48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 21
3. 使用`bytearray()`函数创建字节数组,并打印。
byte_array = bytearray('Hello, World!', 'utf-8')
print(byte_array)
输出结果:
bytearray(b'Hello, World!')
4. 使用`for`循环遍历字节数组并打印每个字节。
byte_array = bytes('Hello, World!', 'utf-8')
for b in byte_array:
print(b, end=' ')
输出结果:
72 101 108 108 111 44 32 87 111 114 108 100 33
以上方法可以帮助你在Python中输出字节数组