I'm trying to understand the way Python displays strings representing binary data.
我试图理解Python显示表示二进制数据的字符串的方式。
Here's an example using os.urandom
这是一个使用os.urandom的例子
In [1]: random_bytes = os.urandom(4)
In [2]: random_bytes
Out[2]: '\xfd\xa9\xbe\x87'
In [3]: random_bytes = os.urandom(4)
In [4]: random_bytes
Out[4]: '\r\x9eq\xce'
In the first example of random_bytes
, after each \x there seem to be values in hexadecimal form: fd a9 be 87.
在random_bytes的第一个例子中,在每个\ x之后似乎是十六进制形式的值:fd a9是87。
In the second example, however, I don't understand why '\r\x9eq\xce'
is displayed.
但是,在第二个例子中,我不明白为什么会显示'\ r \ x9eq \ xce'。
Why does Python show me these random bytes in this particular representation? How should I interpret '\r\x9eq\xce'
?
为什么Python会在这个特定的表示中向我显示这些随机字节?我应该如何解释'\ r \ xeeq \ xce'?
2 个解决方案
#1
10
It's only using the \xHH
notation for characters that are (1) non-printable; and (2) don't have a shorter escape sequence.
它仅对(1)不可打印的字符使用\ xHH表示法; (2)没有较短的逃生顺序。
To examine the hex codes, you could use the binascii
module:
要检查十六进制代码,可以使用binascii模块:
In [12]: binascii.hexlify('\r\x9eq\xce')
Out[12]: '0d9e71ce'
As you can see:
如你看到的:
-
\r
is the same as\x0d
(it's the ASCII Carriage Return character, CR); -
q
is the same as\x71
(the latter is the hex ASCII code of the former).
\ r \ n与\ x0d相同(它是ASCII回车符,CR);
q与\ x71相同(后者是前者的十六进制ASCII码)。
#2
3
\r is a carriage return, q is the q character - you should refer to their ASCII values (0x0d and 0x71)
\ r是回车符,q是q字符 - 你应该参考它们的ASCII值(0x0d和0x71)
Whenever python can - it will display the corresponding ASCII character, you'll only see \x when it can't (usually when the byte is higher than 0x79)
每当python可以 - 它将显示相应的ASCII字符,只有当它不能时才会看到\ x(通常当字节高于0x79时)
#1
10
It's only using the \xHH
notation for characters that are (1) non-printable; and (2) don't have a shorter escape sequence.
它仅对(1)不可打印的字符使用\ xHH表示法; (2)没有较短的逃生顺序。
To examine the hex codes, you could use the binascii
module:
要检查十六进制代码,可以使用binascii模块:
In [12]: binascii.hexlify('\r\x9eq\xce')
Out[12]: '0d9e71ce'
As you can see:
如你看到的:
-
\r
is the same as\x0d
(it's the ASCII Carriage Return character, CR); -
q
is the same as\x71
(the latter is the hex ASCII code of the former).
\ r \ n与\ x0d相同(它是ASCII回车符,CR);
q与\ x71相同(后者是前者的十六进制ASCII码)。
#2
3
\r is a carriage return, q is the q character - you should refer to their ASCII values (0x0d and 0x71)
\ r是回车符,q是q字符 - 你应该参考它们的ASCII值(0x0d和0x71)
Whenever python can - it will display the corresponding ASCII character, you'll only see \x when it can't (usually when the byte is higher than 0x79)
每当python可以 - 它将显示相应的ASCII字符,只有当它不能时才会看到\ x(通常当字节高于0x79时)