为什么是“?”字符出现在以下十六进制值中? (我使用的是 Python 3.5.1)
我的目标是将 string 转换为十六进制值对。
string = b'3f8ccccd'
hexpairs = binascii.unhexlify(string)
我得到的 hepairs 的值是这样的:
b'?\x8c\xcc\xcd'
? 字符的意义是什么?我本以为输出是:
b'\x3f\x8c\xcc\xcd'
请您参考如下方法:
@Benedict 的回答是正确的,这里有一些资源可以帮助您理解:
- binascii.unhexlify()官方 python 文档。
- wikipedia -- ascii table
Originally based on the English alphabet, ASCII encodes 128 specified characters into seven-bit integers as shown by the ASCII chart above.[8] The characters encoded are numbers 0 to 9, lowercase letters a to z, uppercase letters A to Z, basic punctuation symbols, control codes that originated with Teletype machines, and a space. For example, lowercase j would become binary 1101010 and decimal 106. ASCII includes definitions for 128 characters: 33 are non-printing control characters (many now obsolete)[9] that affect how text and space are processed[10] and 95 printable characters, including the space
对于 ? 字符:
- binary: 00111111. 在 python 中,
chr(0b00111111)会输出? - octal: 77. 在 python 中
chr(0o77)会输出? - decimal: 63. 在 python 中
chr(63)会输出? - 十六进制:3F。在 python 中
chr(0x3f)将输出?
如您所见,您可以在 int 之前使用 0b、0o 或 0x 等前缀来让 python 明白你不是在使用十进制数。
但是 0x8C 不在 ascii 表中(因为它从 0 到 127)。 0x8C 是十进制的 140,因此无法显示等效值。
