Sling Academy
Home/Python/Python: 3 ways to convert a string to a hexadecimal value

Python: 3 ways to convert a string to a hexadecimal value

Last updated: May 27, 2023

In Python and the world of software development, a hexadecimal value (or hex value) is a representation of a number using base-16 digits and alphabets. It consists of digits 0-9 and letters A-F. The prefix 0x is optional and sometimes used to indicate that a value is hexadecimal, but it is not required. Hexadecimal values are used for memory addresses, bitwise operations, data encoding, and representing colors in computer graphics, etc.

This succinct and straight-to-the-point article will walk you through several different ways to turn a string into a hexadecimal value in Python. Without any further ado, let’s get started!

Using the hex() method

If you are using Python 3.5 or newer, you can use the encode() and the hex() methods to convert a given string to a hex value with a single line of code.

Example:

text = "Sling Academy"
hex = text.encode("utf-8").hex()
print(hex)

Output:

536c696e672041636164656d79

The syntax is very concise and intuitive. I guess you will like it the most. There is one more thing that I think might help you when using the hex() function. If you want the resulting hex string to be more readable, you can set a custom separator with the sep parameter as shown below:

text = "Sling Academy"
hex = text.encode("utf-8").hex(sep="_")
print(hex)

Output:

53_6c_69_6e_67_20_41_63_61_64_65_6d_79

Using the binascii.hexlify() function

This approach works with both old versions of Python (2.x) and new versions of the programming language (3.x). What we will do are:

  1. Encode the string to bytes using the desired encoding, such as encode("utf-8").
  2. Call the binascii.hexlify() function to convert the bytes to a hexadecimal representation.
  3. Decode the resulting bytes as a string to obtain the hex value.

Code example:

import binascii

string = "Sling Academy"
encoded_bytes = string.encode('utf-8')
hex_value = binascii.hexlify(encoded_bytes).decode('utf-8')

print(hex_value)

Output:

536c696e672041636164656d79

This approach is straightforward, works will all versions of Python and doesn’t rely on any third-party library. However, it requires encoding and decoding steps limited to ASCII characters and might not handle non-ASCII characters well.

Using list comprehension and the ord() function

In this technique, we will convert each character in the string to its corresponding Unicode code point and format it as a hexadecimal value. The steps are:

  1. Iterate through each character in the string.
  2. Use ord() to obtain the Unicode code point of each character.
  3. Format the code point as a hexadecimal string by using the format() function.
  4. Join the hexadecimal strings together with the join() method.

A code example is worth more than a thousand words:

string = "Sling Academy"
hex_value = ''.join([format(ord(char), 'x') for char in string])

print(hex_value) 

Because we use the same input as other examples in this article, the output won’t be a new thing:

536c696e672041636164656d79

The advantage of this technique is that can handle non-ASCII characters and is flexible and customizable. The trade-off is that it requires iterating through each character, which may be less efficient for extremely large strings.

Next Article: Python: 4 Ways to Generate Random Strings

Previous Article: Python: Replace unwanted words in a string with asterisks

Series: Working with Strings in Python

Python

You May Also Like

  • Introduction to yfinance: Fetching Historical Stock Data in Python
  • Monitoring Volatility and Daily Averages Using cryptocompare
  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots