Convert an int to a hex String in Python

In this Python tutorial, we will learn how to convert an int to a hex string.

Table Of Contents

Convert int to a hex string using the hex() function.

The hex() method converts the given integer to a Hexadecimal string.

syntax:

hex(value)

Where parameter value is an integer value and it returns a string containing the hexa decimal version of the given integer value.

Advertisements

Example:

Convert integer 12 to hex string.

# Consider an integer
value=12

print("Original Value: ",value)

# Convert integer to hex
hex_value = hex(value)

print("Hex value: ",hex_value)
print("Type: ",type(hex_value))

Output:

Original Value:  12
Hex value:  0xc
Type:  <class 'str'>

We can say that 12 is converted to hex string – 0xc by checking with the type() method. It returned str, which refers to the string class.

Convert int to a hex string using format() in lowercase

The format() method converts the given value into a hex string.

Syntax:

format(value, '#x')
format(value, 'x')

It takes two parameters and returns the hexadecimal string of given integer.

Parameters:

  1. The value represents the integer value.
  2. The ‘#x’ returns the hex string in lowercase with the prefix- 0x. If we specify it as ‘x’, then it won’t return the prefix- 0x.

Example:

Convert integer value 12 to hex string into lowercase using format() method with and without prefix.

# Consider an integer
value=12

print("Original Value: ",value)

# Convert integer to hex
print("Hex value: ",format(value, '#x'))
# Get the type
print("Type: ",type(format(value, '#x')))

# Convert integer to hex
print("Hex value: ",format(value, 'x'))
# Get the type
print("Type: ",type(format(value, 'x')))

Output:

Original Value:  12
Hex value:  0xc
Type:  <class 'str'>
Hex value:  c
Type:  <class 'str'>

You can see that the hex string is in lowercase.

Convert int to a hex string using format() in uppercase

The format() and convert the given value into a hex string.

Syntax:

format(value, '#X')
format(value, 'X')

It takes two parameters and returns the hexadecimal string of given integer.

Parameters:

  1. The value represents the integer value
  2. The ‘#X’ returns the hex string in the Upper case with prefix- 0X. If we specify it as ‘X’, then it won’t return the prefix- 0X.

Example:

Convert integer value 12 to hex string in uppercase using format() method with and without prefix.

# Consider an integer
value=12

print("Original Value: ",value)

# Convert integer to hex
print("Hex value: ",format(value, '#X'))
# Get the type
print("Type: ",type(format(value, '#X')))

# Convert integer to hex
print("Hex value: ",format(value, 'X'))
# Get the type
print("Type: ",type(format(value, 'X')))

Output:

Original Value:  12
Hex value:  0XC
Type:  <class 'str'>
Hex value:  C
Type:  <class 'str'>

You can see that the hex string is in uppercase.

Convert int to a hex string using f-strings in lowercase

The f-string convert the given value into a hex string.

Syntax:

f'{value:#x}'
f'{value:x}'

Where,
1. The value represents the integer value
2. The ‘#x’ returns the hex string in lowercase with the prefix- 0x. If we specify it as ‘x’, then it won’t return the prefix- 0x.

Example:

Convert integer value 12 to hex string using f-strings with and without prefix.

# Consider an integer
value=12

print("Original Value: ",value)

# Convert integer to hex
strValue = f'{value:#x}'

print("Hex value: ", strValue)
# Get the type
print("Type: ", type(strValue) )

# Convert integer to hex
strValue = f'{value:x}'

print("Hex value: ", strValue)
# Get the type
print("Type: ", type(strValue) )

Output:

Original Value:  12
Hex value:  0xc
Type:  <class 'str'>
Hex value:  c
Type:  <class 'str'>

You can see that the hex string is in lowercase.

Convert int to a hex string using f-strings in uppercase

The f-string convert the given value into a hex string. here we will see how to convert hex string in upper case.

Syntax:

f'{value:#X}'
f'{value:X}'

Where,

  1. value represents the integer value
  2. ‘#X’ returns the hex string in uppercase with prefix- 0X. If we specify it as ‘X’, then it won’t return the prefix- 0X.

Example:

Convert integer value -12 to hex string in uppercase using f-strings with and without prefixes.

# Consider an integer
value=12

print("Original Value: ",value)

# Convert integer to hex
print("Hex value: ",f'{value:#X}')
# Get the type
print("Type: ",type(f'{value:#X}'))

# Convert integer to hex
print("Hex value: ",f'{value:X}')
# Get the type
print("Type: ",type(f'{value:X}'))

Output:

Original Value:  12
Hex value:  0XC
Type:  <class 'str'>
Hex value:  C
Type:  <class 'str'>

You can see that the hex string is in uppercase.

Convert int to a hex string using % operator in lowercase

The % operator will return the value by taking format specifier. If you want to convert an integer to a hex string, you need to specify the format specifier as x. It will get the hex string in lowercase.

Syntax:

"%#x" % value
"%x" % value

Where
1. value represents the integer value
2. ‘#x’ returns the hex string in lowercase with the prefix- 0x. If we specify it as ‘x’, then it won’t return the prefix- 0x.

Example:

Convert integer value -12 to hex string using % operator with and without prefix in lowercase.

# Consider an integer
value=12

print("Original Value: ",value)

# Convert integer to hex
print("Hex value: ","%#x" % value)
# Get the type
print("Type: ",type("%#x" % value))

# Convert integer to hex
print("Hex value: ","%x" % value)
# Get the type
print("Type: ", type("%x" % value))

Output:

Original Value:  12
Hex value:  0xc
Type:  <class 'str'>
Hex value:  c
Type:  <class 'str'>

You can see that the hex string is in lowercase.

Convert int to a hex string using % operator in uppercase

The % operator will return the value by taking format specifier. If you want to convert an integer to a hex string, you need to specify the format specifier as x. It will get the hex string in uppercase.

Syntax:

"%#X" % value
"%X" % value

Where
1. value represents the integer value
2. ‘#X’ returns the hex string in uppercase with prefix- 0X. If we specify it as ‘X’, then it won’t return the prefix- 0X.

Example:

Convert integer value -12 to hex string using % operator with and without prefix in uppercase.

# Consider an integer
value=12

print("Original Value: ",value)

# Convert integer to hex
print("Hex value: ","%#X" % value)
# Get the type
print("Type: ",type("%#X" % value))

# Convert integer to hex
print("Hex value: ","%X" % value)
# Get the type
print("Type: ",type("%X" % value))

Output:

Original Value:  12
Hex value:  0XC
Type:  <class 'str'>
Hex value:  C
Type:  <class 'str'>

You can see that the hex string is in uppercase.

Summary

In this article, we learned about seven different ways to convert int to hex string in python. The x is used to get the hex string in lowercase and X is used to return the hex string in upper case. Thanks.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top