简体   繁体   English

在Python中从int到Byte的转换不以十六进制表示

[英]Int to Byte conversion not representing in hexadecimal in Python

I'm having some difficulty in understanding how python converts between int and byte data types and specifically why it isn't consistent with representing it as hexadecimal numbers. 我在理解python如何在int和byte数据类型之间进行转换时遇到一些困难,尤其是为什么它与将其表示为十六进制数字不一致。

Consider the following where I convert the number 13 into a 2 byte representation: 考虑以下将数字13转换为2字节表示形式的情况:

>>> (13).to_bytes(2, byteorder='big')
b'\x00\r'

Why does it use the character r in the second byte location? 为什么在第二个字节位置使用字符r

In this case I would have expected it to output: 在这种情况下,我期望它输出:

b'\x00\xD'

Doing the reverse in both cases outputs the correct answer. 在两种情况下都做相反的操作将输出正确的答案。

>>> int.from_bytes(b'\x00\x0D', byteorder='big')
13
>>> int.from_bytes(b'\x00\r', byteorder='big')
13

And both have the correct number of bytes 两者都有正确的字节数

>>> len(b'\x00\x0D')
2
>>> len(b'\x00\r')
2

There are some special escape sequences which are so common that they are not explicitly represented with their hex values: 有一些特殊的转义序列非常常见,以致于不能用十六进制值明确表示:

\a <-> \x07  alert
\b <-> \x08  backspace
\t <-> \x09  tab (horizontal)
\n <-> \x0A  new line
\v <-> \x0B  vertical tab
\f <-> \x0C  formfeed
\r <-> \x0D  carriage return
\" <-> \x22  "
\' <-> \x27  '
\\ <-> \x5C  \

you find a full list of escape sequences and how they work here . 您可以在此处找到转义序列的完整列表及其工作方式。 Note that there is also \\0 <-> \\x00 in the C language. 请注意,在C语言中也有\\0 <-> \\x00

There is a difference between bytes and a hexadecimal representation. bytes和十六进制表示之间存在差异。 bytes is a datatype; bytes是一种数据类型; hexadecimal is a way of representing bit patterns on the screen. 十六进制是一种在屏幕上表示位模式的方法。

A bytes is an immutable sequence of 8-bit values. bytes是8位值的不可变序列。 The interpreter displays it, where possible, as characters or as string escape sequences, and where not possible, in hexadecimal. 解释器在可能的情况下将其显示为字符或字符串转义序列,在不可能的情况下以十六进制显示。 The corresponding literal is called a byte string . 相应的文字称为字节字符串 In other words, hexadecimal is a sort of last resort. 换句话说,十六进制是一种不得已的方法。 You can construct the bytes b'ABC' using hexadecimal notation: b'\\x41\\x42\\x43' but the interpreter will still report it as b'ABC' . 您可以使用十六进制表示法构造bytes b'ABC'b'\\x41\\x42\\x43'但解释器仍将其报告为b'ABC' That is no different from the way quotes are handled: 这与处理引号的方式没有什么不同:

>>> a = "ABC"
>>> a
'ABC'
>>> a = 'AB\'C'
>>> a
"AB'C"

The interpreter has a standard way of displaying its data and takes no account of the way you entered that data in the first place. 解释器具有一种显示其数据的标准方式,而无需考虑您最初输入该数据的方式。 This isn't a roundtrip failure, because no information is lost. 这不是往返失败,因为没有信息丢失。 You are seeing an equivalent representation rather than the same representation, is all. 您所看到的就是等效的表示形式,而不是相同的表示形式。

If you want to see a hexadecimal representation then you should ask for it explicitly, instead of relying on the interpreter's default way of displaying a particular datatype. 如果要查看十六进制表示形式,则应明确要求它,而不是依赖于解释器显示特定数据类型的默认方式。

>>> fmt = '\\x{0:02X}\\x{1:02X}'
>>> print(fmt.format(*((13).to_bytes(2, byteorder='big'))))
\x00\0D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM