In this tutorial you will learn how to convert a string to hex in Python, and a hex to string in Python. Show Table of ContentsString to HexThere exist different methods of converting a string into a hex in python. Hexadecimal values have a base of 16, and the prefix 0x is used to display any given string in hexadecimal format. Strings can be converted into a hexadecimal format in one of the following ways. Using encode()The encode() method is one of the most popular methods of converting any string into hex format. In this method, firstly string is converted into bytes using the encode() method, and then the resulting value is converted into hex format using the hex method.
Output: 436f6e76657274696e6720737472696e6720696e746f2068657820666f726d61742e Using ast library methodIn this method, the ast library is used to convert a given string into hex format. Firstly, literal_eval is imported from the ast library. Secondly, a string is created with the prefix 0x because this method only accepts the characters with 0x prefixes. After making the string, it is passed through the literal_eval method. This method gives the integer format of the given string, which can then be passed through the hex method to obtain the hex value of the given string.
Output: 0x569 Using the hex() methodhex() method is generally used to convert the hexadecimal integers string value to hexadecimal values. In this method, a hexadecimal integer is passed as a parameter through the hex() method, and it provides the hexadecimal value of the given string. For this method to work, the given string should be converted into a hexadecimal integer value, and then the integer value should be passed through the hex function. Any given string can be converted into a hexadecimal integer value by passing it through the int method along with the base 16 as a parameter.
Output: 0x64533490 Sometimes you will encounter syntax errors and typeErrors because the hex method only accepts integer values and if you pass a string through it, then it will give an error. Another fastest way of converting a string into a hexadecimal value is using b with hex method. In this method, “b” is placed at the beginning of a string which indicates the conversion of the string into bytes. Generally, hex values start with 0x, so the prefix “0x” can be placed with the output to get the hexadecimal value of any given string.
Output: 0x436f6e76657274696e6720737472696e6720696e746f2068657820666f726d6174 Using binasciiIn this method, binascii library is used to convert any given string into hex format. Firstly, we import the binascii module. Secondly, the given string is converted into a byte object using b. Finally, the byte object is passed through the hexlify method, which gives the hex value of the given string.
Output: b'48656c6c6f' If the purpose is just to convert the strings of the alphabet into the hexadecimal format, then the ord method along with the hex method can be applied to get the hex value of any given alphabet. 436f6e76657274696e6720737472696e6720696e746f2068657820666f726d61742e0 Output: 436f6e76657274696e6720737472696e6720696e746f2068657820666f726d61742e1 Hex to StringHexadecimal string and ASCII values in python are interchangeable and different methods can be used to convert a given hex value to a string and vice versa. Using decode() methodIn this method, bytearray.decode (encoding, error) takes the byte array as an input and then decodes it using the encoding specified as an argument. In order to decode a hex value, the first step is to convert the hex string into a byte string and then apply the bytearray.decode() method. To convert hex string into bytes, bytearray.fromhex() method can be used. 436f6e76657274696e6720737472696e6720696e746f2068657820666f726d61742e2 Output: 436f6e76657274696e6720737472696e6720696e746f2068657820666f726d61742e3 Using codecs.decode() methodIn this method, codecs module is imported to convert the hex values into strings. This method requires a codecs library for conversion, which contains base classes for encoding and decoding data, commonly used in Unicode text-based files. This method is similar to the decode() method. The only difference is that along with encoding and error arguments, this method also takes the objects as input arguments. Error argument in this method is used to handle errors during the execution of the program. The codecs.decodes() method in this case returns a byte object, which is then converted into the string using the str() method. 436f6e76657274696e6720737472696e6720696e746f2068657820666f726d61742e4 Output: 436f6e76657274696e6720737472696e6720696e746f2068657820666f726d61742e5 By appending hex to a stringIn this method, a hex value is converted into a string and then combined with the other string. It’s an efficient one-liner that reads in a single hex value at a time converts it to an ASCII character and appends it to the end of the variable. This repeats until the conversion is complete. 436f6e76657274696e6720737472696e6720696e746f2068657820666f726d61742e6 Output: 436f6e76657274696e6720737472696e6720696e746f2068657820666f726d61742e7 Using the binascii moduleIt is one of the simplest ways of converting a hex value to a string format. In this method, the unhexlify method of the binascii module is used for the conversion of hex value to string format. 436f6e76657274696e6720737472696e6720696e746f2068657820666f726d61742e8 Output: 436f6e76657274696e6720737472696e6720696e746f2068657820666f726d61742e9 Hex to integerThe int constructor 2 can be used for conversion between a hex string and an integer. The int constructor takes the string and the base you are converting from and will give the corresponding integer value of given hex. 0Output: 1ConclusionThere are many ways to convert strings to hexadecimals, and hexadecimals to strings in Python. A practical use case for these conversion methods could be obtaining character codes being read in from files. |