python 整数 1字节_Python程序打印代表整数的字节数组
python 整數(shù) 1字節(jié)
Given an integer number and we have to convert it into a byte array in Python.
給定一個整數(shù),我們必須在Python中將其轉(zhuǎn)換為字節(jié)數(shù)組。
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.
要將整數(shù)轉(zhuǎn)換為字節(jié)(字節(jié)數(shù)組) ,我們使用int類的to_bytes()方法 ,將其與帶有三個參數(shù)的數(shù)字一起調(diào)用,并返回表示該數(shù)字的字節(jié)數(shù)組。
Syntax:
句法:
int.to_bytes(size, byteorder)Here,
這里,
size is the maximum size (in bytes) of the number.
size是數(shù)字的最大大小(以字節(jié)為單位)。
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是一種打印字節(jié)的技術(shù),它具有兩個值,分別大值以big-endian格式打印字節(jié)數(shù)組和很少值以little-endian格式打印字節(jié)數(shù)組。
Example:
例:
Input:num = 100# function callprint(num.to_bytes(2, byteorder ='big'))Output:b'\x00d'Python代碼將整數(shù)轉(zhuǎn)換為字節(jié)數(shù)組 (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 = 0b1000011Second run: Enter an integer number: 3 bits required to store 3 = 2 binary value of 3 is = 0b11If number is longer than 2 bytes value,
如果數(shù)字大于2個字節(jié)的值,
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.
如果輸入數(shù)字大于2個字節(jié),并且我們使用to_bytes()方法使用大小“ 2個字節(jié)” ,則會發(fā)生“ OverflowError”錯誤( “ int太大而無法轉(zhuǎn)換” )。
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 convertto_bytes()數(shù)組,大小為“ 4個字節(jié)” (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個字節(jié)” ,如果數(shù)字大于它,則可以增加大小。 在此示例中,這里我們使用的大小為“ 4 bytes” ,因此,我們可以將整數(shù)轉(zhuǎn)換為4個字節(jié)。
# 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數(shù)組 (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.
要打印一個整數(shù)的字節(jié)數(shù)組,我們可以使用little定義字節(jié)序值。 在此示例中,我們將一個整數(shù)(至多4個字節(jié))以little-endian順序轉(zhuǎn)換為bytes數(shù)組。
# 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 整數(shù) 1字節(jié)
總結(jié)
以上是生活随笔為你收集整理的python 整数 1字节_Python程序打印代表整数的字节数组的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 钝器流剑宗远古幻影9件魔法石和项链 选红
- 下一篇: puppeteer api_使用Node