Convert Bytearray to Hexadecimal String - Python Last Updated : 19 Apr, 2025 Comments Improve Suggest changes Like Article Like Report We are given a byte array a=bytearray([15, 255, 100, 56]) we need to convert it into a hexadecimal string that looks like: 0fff6438. Python provides multiple ways to do this:Using the .hex() method (built-in)Using binascii.hexlify()Using format() with join()Let’s explore each method with examples.Using hex() MethodPython’s bytearray and bytes objects come with a .hex() method that returns a lowercase hexadecimal string for the bytes, without any prefix or separator. Python a = bytearray([15, 255, 100, 56]) h = a.hex() print(h) Output0fff6438 Explanation:.hex() method automatically converts each byte to a two-digit hexadecimal value.All the hex values are joined into one continuous string, no separators, no prefix.Using binascii.hexlify()binascii.hexlify() function converts bytes or bytearray into their hexadecimal representation, but returns the result as a bytes object, which you can decode to get a string. Python import binascii a = bytearray([15, 255, 100, 56]) b = binascii.hexlify(a).decode() print(b) Output0fff6438 Explanation:binascii.hexlify(a) gives b'0fff6438'..decode() turns the bytes into a normal string.Note: This method is helpful when working with low-level data formats (e.g., networking or cryptography).Using format() and join()We can loop through each byte, format it to a two-character hex using format(byte, '02x'), and then combine the results using ''.join(). Python a = bytearray([15, 255, 100, 56]) h = ''.join(format(byte, '02x') for byte in a) print(h) Output0fff6438 Explanation:format(byte, '02x') ensures each byte is converted to a two-digit hexadecimal string (padded with zero if needed).join() merges them into one continuous string.Also read: format(), join(), hex(). Comment More infoAdvertise with us Next Article Convert Bytearray to Hexadecimal String - Python M manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python Program to Convert Binary to Hexadecimal Given a binary number, the task is to write a Python program to convert the given binary number into an equivalent hexadecimal number. i.e convert the number with base value 2 to base value 16. In hexadecimal representation we 16 values to represent a number. Numbers 0-9 are expressed by digits 0-9 4 min read Convert String to bytes-Python The goal here is to convert a string into bytes in Python. This is essential for working with binary data or when encoding strings for storage or transmission. For example, given the string "Hello", these methods can convert it into a byte representation like b'Hello'. Letâs explore different method 2 min read How to Create String Array in Python ? To create a string array in Python, different methods can be used based on the requirement. A list can store multiple strings easily, NumPy arrays offer more features for large-scale data and the array module provides type-restricted storage. Each method helps in managing collections of text values 2 min read Python program to convert hex string to decimal Converting a hexadecimal string to a decimal number is a common task in programming, especially when working with binary data or low-level computations. Hexadecimal values offer a concise way to represent large numbers.Using the int() FunctionUsing the int() function is the most common and straightf 2 min read Convert list to Python array - Python We can use a number of approaches to convert a list into a Python array based on the requirements. One option is to use the array module, which allows us to create arrays with a specified data type. Another option is to use the numpy.array() method, which provides more features for working with arra 2 min read Like