python 整数 1字节
Given an integer number and we have to convert it into a byte array in Python.
给定一个整数,我们必须在Python中将其转换为字节数组。
To convert an integer number into bytes (byte array), we use to_bytes() method of int class, it is called with the number with three arguments and returns a byte array representing the number.
要将整数转换为字节(字节数组) ,我们使用int类的to_bytes()方法 ,将其与带有三个参数的数字一起调用,并返回表示该数字的字节数组。
Syntax:
句法:
int.to_bytes(size, byteorder)
Here,
这里,
size is the maximum size (in bytes) of the number.
size是数字的最大大小(以字节为单位)。
byteorder is the technique to print the bytes, it has two values big to print bytes array in big-endian format and little to print bytes array in little-endian format.
byteorder是一种打印字节的技术,它具有两个值,分别大值以big-endian格式打印字节数组和很少值以little-endian格式打印字节数组。
Example:
例:
Input:
num = 100
# function call
print(num.to_bytes(2, byteorder ='big'))
Output:
b'\x00d'
Python代码将整数转换为字节数组 (Python code to convert an integer number to bytes array )
# Python program to find number of bits
# necessary to represent an integer in binary
# input a number
num = int(input("Enter an integer number: "))
# total bits to represent number
bits = num.bit_length()
print("bits required to store ", num, " = ", bits)
print("binary value of ", num, " is = ", bin(num))
Output
输出量
First run:
Enter an integer number: 67
bits required to store 67 = 7
binary value of 67 is = 0b1000011
Second run:
Enter an integer number: 3
bits required to store 3 = 2
binary value of 3 is = 0b11
If number is longer than 2 bytes value,
如果数字大于2个字节的值,
If the input number is larger than 2 bytes and we used size "2 bytes" with the to_bytes() method, then an "OverflowError" error ("int too big to convert") will occur.
如果输入数字大于2个字节,并且我们使用to_bytes()方法使用大小“ 2个字节” ,则会发生“ OverflowError”错误( “ int太大而无法转换” )。
Enter an integer number: 12387615
Traceback (most recent call last):
File "/home/main.py", line 8, in <module>
x = num.to_bytes(2, byteorder ='big')
OverflowError: int too big to convert
to_bytes()数组,大小为“ 4个字节” (to_bytes() array with size "4 bytes")
In the previous example, we used size "2 bytes", if the number is larger than it, we can increase the size. Here, in this example – we are using size "4 bytes", thus, we can convert an integer till 4 bytes.
在前面的示例中,我们使用大小“ 2个字节” ,如果数字大于它,则可以增加大小。 在此示例中,这里我们使用的大小为“ 4 bytes” ,因此,我们可以将整数转换为4个字节。
# Python program to print an array of bytes
# representing an integer
# input an integer number
num = int(input("Enter an integer number: "))
# finding the byte array
x = num.to_bytes(4, byteorder ='big')
# printing byte array
print("x = ", x)
Output
输出量
First run:
Enter an integer number: 12387615
x = b'\x00\xbd\x05\x1f'
Second run:
Enter an integer number: 9999876
x = b'\x00\x98\x96\x04'
以little-endian格式打印bytes数组 (Printing the bytes array in little-endian format)
To print bytes array of an integer number, we can define byteorder value with little. In this example, we are converting an integer number (till 4 bytes) to bytes array in little-endian order.
要打印一个整数的字节数组,我们可以使用little定义字节序值。 在此示例中,我们将一个整数(至多4个字节)以little-endian顺序转换为bytes数组。
# Python program to print an array of bytes
# representing an integer
# input an integer number
num = int(input("Enter an integer number: "))
# finding the byte array
x = num.to_bytes(4, byteorder ='little')
# printing byte array
print("x = ", x)
Output
输出量
First run:
Enter an integer number: 12387615
x = b'\x1f\x05\xbd\x00'
Second run:
Enter an integer number: 9999876
x = b'\x04\x96\x98\x00'
翻译自: https://www.includehelp.com/python/print-an-array-of-bytes-representing-an-integer.aspx
python 整数 1字节