Python字节字面量具有非十六进制的额外字符,但会更改字符串的值 [英] Python bytes literal has extra characters that aren't hex, but alter the value of the string

查看:115
本文介绍了Python字节字面量具有非十六进制的额外字符,但会更改字符串的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经习惯了将字节表示为十六进制值(例如 b'\x7a'是十六进制值 0x7a <)的python字节文字语法。 / code>。但是,我遇到了一个问题,我不知道它是如何工作的。我第一次使用ssl库,并使用 ssl.RAND_bytes(...)创建随机值。它返回的字符串带有会改变值的字符(例如 int.from_bytes(...)
我收到的示例字符串:

I am used to the python byte literal syntax representing bytes as hex values such as b'\x7a' is the hex value 0x7a. I however have run into an issue that I don't know how it is working. I am using the ssl library for the first time and creating random values with ssl.RAND_bytes(...) It is returning strings with characters that alter the value (say when doing an int.from_bytes(...)) Example strings I have received:

b'\x12\x1f)\x8b\xe0\xd7LD'
b'\x808\x8a(\x02\xb3S\xc9\xabW_\n\xf3\xbb\x80o'
b'\xde\xef\x81%FMB=Ps'
b'9\x81\x90\xdc\xfa\x98\xbd\x1f^;'

我不明白的是那些非十六进制值的字符,例如 \x81 前的 9 MB = Ps 。我想了解这些额外的值代表什么,以及在调试代码时如何解释它们。我知道python与这些无关,是我不知所措。

What I don't understand are those non hex value characters, such as the 9 preceding the \x81 or the MB=Ps. I would like to understand what these extra values represent and how I can interpret them when debugging my code. I know python has no issue with these, it is me who is at a loss.

注意:我在Windows 7 64bit上使用python3.3

Note: I am on python3.3 on windows 7 64bit

推荐答案

字节数组表示法将某些值转换为其文本表示形式

Byte array notation converts certain values to their textual representation:

>>> b'\x12\x41\x42'
b'\x12AB'

在这种情况下,十六进制数字41是大写字母A的ASCII值。

In this case the hex number 41 is the ASCII value for capital letter A.

这篇关于Python字节字面量具有非十六进制的额外字符,但会更改字符串的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆