Open In App

Python program to print an array of bytes representing an integer

Last Updated : 24 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer N, the task is to write a Python program to represent the bytes of this number as an array. 

A byte is a group of 8 bits. Any integer can be represented in the form of bytes and bits. We generally use hexadecimal codes to represent a byte. A single hexadecimal character can represent 4 bits so a pair of hexadecimal characters are used to represent a byte. 

Example:

Input: N = 543

Output: [‘0x2’, ‘0x1f’]

Explanation: 543 can be represented as 1000011111 in binary which can be grouped as (10)(00011111), each group representing a byte. In hexadecimal form, this number will be (0x02)(0x1F).

Input: N = 17292567

Output: [‘0x1’, ‘0x7’, ‘0xdd’, ‘0x17’]

Explanation: 17292567 can be represented as 1000001111101110100010111 in binary which can be grouped as (1)(00000111)(11011101)(00010111), each group representing a byte. In hexadecimal form, this number will be (0x1), (0x7), (0xdd), (0x17).

Method 1 (Manual conversion):

Just like we convert decimal numbers to binary numbers, we can convert it into a base-256 number which will give us 8-bit numbers that represent bytes for the given number. 

Below is the code to implement the above-discussed method:

Python3




n = 17292567
  
# Initialize the empty array
array = []
  
# Get the hexadecimal form
while(n):
    r = n % 256
    n = n//256
    array.append(hex(r))
      
# Reverse the array to get the MSB to left
array.reverse()
print(array)


Output

['0x1', '0x7', '0xdd', '0x17']

Method 2 (Using to_bytes() method):

We can also use the to_bytes(length,byteorder) method to convert the number into a hexadecimal string. But the problem with this function is that while printing, the hex codes can convert to their corresponding characters in ASCII coding scheme. To overcome this, we can use hex() method over this method. It takes two arguments, ‘length‘ is the size of the array and ‘byteorder’ is the ordering of bytes where “big” means MSB will be on left and “little” means MSB will be on right.

Below is the code to implement the above-discussed method:

Python3




import math
  
  
n = 543
  
# Calculate the length of array
size = int(math.log(n, 256))+1
  
# Use the method to_bytes() with "big" 
# or "little" property We need to apply
# hex() method to avoid the conversion 
# into ASCII letters
hexForm = n.to_bytes(size, "big").hex()
  
# Append 8 bits together ie pair of 4 bits to get a byte
array = []
for i in range(0, len(hexForm), 2):
    array.append('0x'+hexForm[i]+hexForm[i+1])
print(array)


Output

['0x02', '0x1f']

Method 3 (Using string formatting commands):

In Python, there is a string command “%x” that can convert the given integer into hexadecimal format. We can use this to get the desired output.

Below is the code to implement the above-discussed method:

Python3




n = 8745
  
# Get string representation of hex code
hexcode = "%x" % n
  
# Pad an extra 0 is length is odd
if len(hexcode) & 1:
    hexcode = "0"+hexcode
  
array = []
for i in range(0, len(hexcode), 2):
    array.append('0x'+hexcode[i]+hexcode[i+1])
print(array)


Output

['0x22', '0x29']


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads