In this article, I have explained multiple ways of converting hexadecimal strings to regular strings in Python by using omhex(), binascii, list comprehension, and codecs modules with examples. Print("After converting hex to string:", result) Print("Hexa decimal string:", hex_string) The codecs module provides additional functionality for encoding and decoding data, and in this case, it simplifies the process of converting a hexadecimal string to bytes and then to a regular string. Using UTF-8 encoding you can decode the bytes into a regular string. For example, the code() function directly decodes the hexadecimal string into bytes. You can also use the codecs module in Python to convert a hexadecimal string to a regular string. The final result will print on the console as a regular string. For example, first, initializes a hexadecimal string and converts it to bytes using omhex(), and then decodes the bytes to a string using UTF-8 encoding. You can convert a hexadecimal string to a regular string using the omhex() method. # To convert hexadecimal string to normal stringīyte_string = bytes(hex_string, encoding='utf-8')īinary_string = code(byte_string, "hex") # Example 5: Using code() to convertīyte_string = code(hex_string, 'hex') # Example 4: Another example to convert hex to string # Convert hexadecimal string to normal stringīyte_data =, 16) for i in range(0, len(hex_string), 2)] The easiest way to do it in Python 3.5 and higher is: > 'halo'.encode().hex() '68616c6f' If you manually enter a string into a Python Interpreter using the utf-8 characters, you can do it even faster by typing b before the string: > b'halo'.hex() '68616c6f' Equivalent in Python 2.x: > 'halo'. More available characters and byte sizes can be found here. H is equal to two bytes (16 bit) unsigned. # To convert hexadecimal string to stringīyte_string = binascii.unhexlify(hex_string) As I specified little endian (using the '<' char) at the start of the format string the function returned the decimal equivalent. # Example 2: Using binascii.unhexlify() method The data is UTF-8 encoded bytes escaped with URL quoting, so you want to decode, with (), which handles decoding from percent-encoded data to UTF-8 bytes and then to text, transparently: from urllib. # to convert hexadecimal to normal string
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |