Python program to print an array of bytes representing an integer

Python example of int.to_bytes() strategy: Here, we will figure out how to change over a given number into a byte exhibit?

Given an integer number and we need to change over it into a byte cluster in Python.

To change over an integer number into bytes (byte cluster), we use to_bytes() strategy for int class, it is called with the number with three contentions and returns a byte exhibit speaking to the number.

Syntax:

    int.to_bytes(size, byteorder)

Here,

  • size is the most extreme size (in bytes) of the number.
  • byteorder is the method to print the bytes, it has two values huge to print bytes cluster in large endian configuration and little to print bytes exhibit in a little-endian group.

Example:

    Input:
    num = 100

    # function call
    print(num.to_bytes(2, byteorder ='big'))

    Output:
    b'\x00d'

Python code to change over an integer number to bytes exhibit:

# 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

In the event, that number is longer than 2 bytes value,

In the event that the info number is bigger than 2 bytes and we utilized size “2 bytes” with the to_bytes() technique, at that point an “OverflowError” mistake (“int too huge to change over”) will happen.

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() exhibit with size “4 bytes”

In the past example, we utilized size “2 bytes”, if the number is bigger than it, we can build the size. Here, in this example – we are utilizing size “4 bytes“, hence, we can change over an integer till 4 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 ='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'

Printing the bytes exhibit in little-endian design:

To print bytes cluster of an integer number, we can characterize byteorder value with pretty much nothing. In this example, we are changing over an integer number (till 4 bytes) to bytes cluster in the little-endian request.

# 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'

Leave a Comment

error: Alert: Content is protected!!