# How to Convert String to HEX in Python [Best Methods]

Hexadecimal or HEX in short is a base-16 numbering system that uses 16 different symbols (0-9 and A-F) to represent any value. Converting a string to hexadecimal has various applications like encoding data for secure transmission or generating unique identifiers, etc. In Python, there are multiple methods to convert string to HEX value. In this article, I will share the best 8 methods among them.

Course for You: Learn Python in 100 days of coding

## Application of String to HEX Conversion

Converting strings to hexadecimal has various real-world applications across different domains. Such as:

Data Encoding and Serialization: Converting strings to hexadecimal is a common technique for encoding data before transmission, especially in network communication. You may observe that this encoding method is mentioned in WhatsApp. They call it end-to-end chat encryption.

Hashing and Cryptography: Hashing is a technique to convert simple text to special code made up of numbers and letters, called hexadecimal. This is very important when you want to keep passwords safe and create digital signatures. The hexadecimal representation is often used as part of cryptographic processes.

Encoding Non-Text Data: Hexadecimal representation is a way to convert not just regular words but also other types of computer data, like binary (0s or 1s) or machine code instructions. This is commonly used in low-level programming and hardware communication.

## Method 1: Using the `str.encode()` Method

You can use built-in method called `str.encode()` in Python to convert string to a byte. Then convert these bytes to a hexadecimal representation using `hex()` method.

```# Convert String to hexadecimal or hex using str.encode() and hex method
# Define a string
text = "Hello, World!"

# Convert the string to bytes
bytes_text = text.encode()

```
``Hexadecimal representation: 48656c6c6f2c20576f726c6421``

## Method 2: Using a Custom Function

To convert a string to hexadecimal (or HEX) without encoding it to bytes, you can use a custom function. In this method, we iterate through each character in the string then use the `ord()` function to get the Unicode code point of the character, and then convert it to hexadecimal using `hex()`. Finally, remove the ‘0x’ prefix using slicing.

```# How to String to hexadecimal or hex using custom function
# Define a function to convert a string to hexadecimal
def string_to_hex(input_string):
hex_string = ""
for char in input_string:
hex_string += hex(ord(char))[2:]  # Convert each character to its hexadecimal representation

return hex_string

# Define a string
text = "Python is awesome!"

# Convert the string to hexadecimal

```
``Hexadecimal representation: 507974686f6e20697320617765736f6d6521``

## Method 3: Using List Comprehension

This method is similar to the custom function. The only difference is it uses list comprehension to convert a string to Hex in Python.

```# Define a string
text = "Simple Example"

# Convert the string to hexadecimal using list comprehension
hexadecimal_text = ''.join([hex(ord(char))[2:] for char in text])

```

## Method 4: String to HEX with Fixed Length

If you want to convert a Python string to a HEX format with a fixed length, you can use string formatting to achieve this.

```# python string to hex with fixed length

# Define a string
text = "Hello, World!"

# Define the desired fixed length
fixed_length = 16

# Convert the string to hexadecimal with fixed length

# If the hex representation is shorter than the fixed length, you can pad it with zeros to the left

# Print the fixed-length hexadecimal representation
```
``Fixed-length hexadecimal representation: 48656c6c6f2c2057``

## Method 5: String to HEX with Leading Zeros

If you want to convert a Python string to a hexadecimal (hex) number with leading zeros, you can use the `str.format()` method or f-strings. This Python code will only work in Python 3.6 or later versions.

Also Read:  How to Run Multiple Python Scripts Simultaneously?

#### Using `str.format()` method:

```# Define a string
text = "42"

```
``Hexadecimal representation with leading zeros: 2a``

#### Using f-strings (Python 3.6 and later):

```# Define a string
text = "42"

# Convert the string to hexadecimal with leading zeros using an f-string

```
``Hexadecimal representation with leading zeros: 2a``

## Method 6: String to Hex Converter of 32 bits

Let’s say now you want to convert a Python string to a 32-bit hexadecimal value (32 bits in length). To do this first, we need to convert the string to its hexadecimal representation using `text.encode().hex()`. Then, we check if the length is less than 8 characters (32 bits). If it is, we pad it with zeros to the left using `zfill(8)` to ensure it’s a 32-bit hexadecimal representation.

```# Define a string
text = "Hello, World!"

# Convert the string to a 32-bit hexadecimal representation

# If the length is less than 32 bits, pad with zeros to the left

# Print the 32-bit hexadecimal representation
```
``32-bit hexadecimal representation: 48656c6c6f2c20576f726c6421``

## Method 7: base64.decode String into Hex

You can also use base64.decode() function from base64 library to convert a string into ascii hex code in Python. Below is the code to decode a Base64-encoded string into a hexadecimal (or hex) string in Python.

```import base64

# Define a Base64-encoded string
base64_encoded_string = "SGVsbG8sIFdvcmxkIQ=="

# Decode the Base64-encoded string
decoded_bytes = base64.b64decode(base64_encoded_string)

# Convert the decoded bytes to a hexadecimal representation

```
``Hexadecimal representation: 48656c6c6f2c20576f726c6421``

## Method 8: String to Hex Array

If you want to convert a Python string to a hexadecimal (HEX) array, you can use a list comprehension. This will create an array of hexadecimal values representing each character in the string.

```# Define a string
text = "Hello, World!"

# Convert the string to a list of hexadecimal values
hex_array = [hex(ord(char))[2:] for char in text]

``Hexadecimal array: ['48', '65', '6c', '6c', '6f', '2c', '20', '57', '6f', '72', '6c', '64', '21']``