日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 整数 1字节_Python程序打印代表整数的字节数组

發布時間:2023/12/1 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 整数 1字节_Python程序打印代表整数的字节数组 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 callprint(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 = 0b1000011Second 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字節

總結

以上是生活随笔為你收集整理的python 整数 1字节_Python程序打印代表整数的字节数组的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。