Python program to print an array of bytes representing an integer
Last Updated :
24 Jan, 2021
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)
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)
Similar Reads
Python program to Represent Negative Integers in binary format Given a Negative Integer, the task is to write a Python program to convert the integer into binary format. Examples: Input: -14 Output: -1110 Input: -12 Output: -1100 Input: -32 Output: -100000 Let's implement this for the number in different ways : Method 1: Using format specifier 'b' In format spe
1 min read
Python program to convert a byte string to a list of integers We have to convert a byte string to a list of integers extracts the byte values (ASCII codes) from the byte string and stores them as integers in a list. For Example, we are having a byte string s=b"Hello" we need to write a program to convert this string to list of integers so the output should be
2 min read
Python program to print number of bits to store an integer and also the number in Binary format Given an integer, the task is to write a Python program to print the number of bits to store that integer and also print the same number in Binary format. Example: Input: n = 10 Output: Number of bits to store the number: 4 Binary value: 0b1010 Input: n = 120 Output: Number of bits to store the numb
4 min read
Python Program to Convert any Positive Real Number to Binary string Given any Real Number greater than or equal to zero that is passed in as float, print binary representation of entered real number. Examples: Input: 123.5 Output: 1 1 1 1 0 1 1 . 1 Input: 0.25 Output: .01 Mathematical Logic along with steps done in programming: Any real number is divided into two pa
4 min read
Python program to print the binary value of the numbers from 1 to N Given a positive number N, the task here is to print the binary value of numbers from 1 to N. For this purpose various approaches can be used. The binary representation of a number is its equivalent value using 1 and 0 only. Example for k = 15, binary value is 1 1 1 1 WORKING FOR METHOD 1Method 1: U
2 min read
Integer to Binary String in Python We have an Integer and we need to convert the integer to binary string and print as a result. In this article, we will see how we can convert the integer into binary string using some generally used methods.Example: Input : 77Output : 0b1001101Explanation: Here, we have integer 77 which we converted
4 min read
Python Program to Convert a Number into 32-Bit Binary Format Binary representation is a fundamental concept in computer science, and converting numbers into binary format is a common task for programmers. In this article, we will explore some simple and generally used methods to convert a number into a 32-bit binary format using Python. Each method provides a
2 min read
Python program to convert binary to ASCII In this article, we are going to see the conversion of Binary to ASCII in the Python programming language. There are multiple approaches by which this conversion can be performed that are illustrated below: Method 1: By using binascii module Binascii helps convert between binary and various ASCII-en
3 min read
Python program to convert ASCII to Binary We are having a string s="Hello" we need to convert the string to its ASCII then convert the ASCII to Binary representation so that the output becomes : 01001000 01100101 01101100 01101100 0110111. To convert an ASCII string to binary we use ord() to get the ASCII value of each character and format(
2 min read
Python program to print the hexadecimal value of the numbers from 1 to N Given a number N, the task is to write a Python program to print the hexadecimal value of the numbers from 1 to N. Examples: Input: 3 Output: 1 2 3 Input: 11 Output: 1 2 3 4 5 6 7 8 9 a bApproach: We will take the value of N as input.Then, we will run the for loop from 1 to N+1 and traverse each "i"
5 min read