Convert an integer or float to hex in Python

Convert an integer or float to hex in Python:

In this tutorial, we will learn how to find out the hex value of an integer or a float in Python.

Hexadecimal is a base-16 number system. It uses the numbers 0 to 9 and then uses the letters A to F to represent any number. Numbers from zero to nine are represented by ‘0’ to ‘9’ similar to Decimal numbers. The letters ‘A’ to ‘F’ are used to represent the values from ten to fifteen. So, 10 in Decimal is equal to A in Hexadecimal.

To convert a decimal value to hexadecimal, Python provides two inbuilt methods. By using these methods, we can find out the hexadecimal representation of an integer or a floating point number.

In this blog post, we will learn how to generate the hexadecimal value of an integer or float value.

hex() method:

The hex() method is used to find out the hex value of an integer. It converts an integer number to a lowercase hexadecimal string. The hexadecimal string is prefixed with 0x. One more thing to note is that we can even find out the hexadecimal value of a negative integer. The syntax of this method is:

hex(x)

It takes only one parameter. The parameter should be an integer or it should define an index() method that returns an integer. It will convert the provided integer value to hexadecimal and returns the result in string format.

Let me show you with one example of how it works:

print("Hex value of 2 is ",hex(2))
print("Hex value of 3 is ",hex(3))
print("Hex value of 16 is ",hex(16))
print("Hex value of 7 is ",hex(7))
print("Hex value of 21 is ",hex(21))

Download it on GitHub

python int float to hex

python convert integer to hex

As you can see that 0x is prefixed with all strings to indicate that the string is a hexadecimal value. Now, let’s try to find out the hex value of a floating point number with the hex method:

python int float to hex

It throws one TypeError.

How to find the hexadecimal values of floating point numbers:

As we have seen above, we cannot use the hex() method to find the hex values for float numbers. To find the hexadecimal value for floating point numbers in Python, we can use the float.hex() method. This method takes one float as the argument and returns the hexadecimal string.

Example:

The following example shows how to use the float.hex method to find the hexadecimal value of float numbers:

print("Hex value of 2.4 is ",float.hex(2.4))
print("Hex value of 21.14 is ",float.hex(21.14))
print("Hex value of 2.41 is ",float.hex(2.41))
print("Hex value of 10.4 is ",float.hex(10.4))

Download it on GitHub

python int float to hex

The float.hex() method is used to convert a floating point number to hexadecimal.

Similarly, we can use the float.fromhex() method to convert a hexadecimal string value to floatingpoint. The hex() is an instance method but fromhex() is a class method.

Conclusion:

We can use the above methods to create one simple hexadecimal converter in python.

  1. Create one python file for the converter.

  2. Ask the user to enter a number.

  3. Check if it is a valid number or not. If not, print one error message and exit.

  4. Check if the number is an integer number or floating-point number.

  5. If it is an integer, use the hex(x) method and if it is a float, use the float.hex() method to find out the hex value.

  6. Print out the result to the user.

If you want to add any other ways to convert an integer/float value to hexadecimal, please raise a pull request on GitHub.

Similar tutorials: