Python program to print an array of bytes representing an integer

Python example of int.to_bytes() method: Here, we are going to learn how to convert a given number into a byte array?
Submitted by IncludeHelp, on April 16, 2019

Given an integer number and we have to convert it into a byte array in 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.

Syntax:

    int.to_bytes(size, byteorder)

Here,

  • size is the maximum size (in bytes) of the number.
  • 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.

Example:

    Input:
    num = 100

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

    Output:
    b'\x00d'

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,

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.

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() 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.

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

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

Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.