. Advertisement .
. Advertisement .
A hexadecimal string is one of the most useful tools in a programmer’s arsenal. However, that does not mean that we should deal with it all the time. It is very beneficial that you can immediately convert hex to in with Python whenever there is a need.
That is why we prepared this article. We also threw in some special cases so that you can have an easier time.
Using int() To Convert Hex To Int With Python
Before, you may need to replace characters in the string. Now, the most effective and common practice to convert hex to int with Python is through a type-casting int() function. Here is its syntax:
There are two arguments needed for the function to work. The x variable signifies the number of strings being converted with a default value of 0. On the other hand, the base variable lets the machine know of the base of x. It’s either 0, which means code literal, or a value between 2 and 36.
To be more specific, if the base variable is assigned a value of 2, it means the format will be binary. In the same sense, 8 leads to octal, 16 changes it to hexadecimal.
This function’s output is an integer object converted from the string input. If there is no parameter given, the return will be 0. When the base is not specified, it will default to 10 and convert the output accordingly.
Some strings already have a prefix to inform the computer of their nature. For example, a hex string usually has a 0x prefix. If you encounter this type of string, remember to keep the base value as 0.
Doing so ensures that int() can automatically detect the string’s prefix and function correctly. There is nothing wrong with passing 16 to the base. However, passing 0 can save a lot of time, as the computer can automatically detect and change.36
This is especially helpful if you want to deal with various values sporting different formats.
If you don’t see any prefix in the hexadecimal string, simply specify the function’s base value as 16.
example_HexVal = 'beef101' example_HexVal1 = '0xbeef101' print(int(example_HexVal, 16)) print(int(example_HexVal1, 0)) print(int(example_HexVal1, 16))
200208641 200208641 200208641
Convert Hex To Int With Python – Little And Big-Endian Orders
There are two popular ordering systems for hexadecimal strings, big and little-endian. For the most part, a hexadecimal string follows the little-endian order, putting the most significant number at the sequence’s right-most part. The big-endian order does the opposite, making it not as widely used.
That is why you need to first figure out a way to convert the big-endian hex string to little-endian. Only then can you start converting it to an integer value with the usual approach that we discussed above.
The best solution for this problem is to use bytearray.fromhex() to obtain a result, then utilize reverse() on it. Finally, you can use the method above to finish the job. Below is a code snippet demonstrating how it works.
bigEndian = 'efbe' littleEndian = bytearray.fromhex(bigEndian) littleEndian.reverse() print(int(littleEndian, 16))
In conclusion, this article has covered all vital information on how to convert hex to int with Python. We also showed you what to do in cases where the hex strings have varying formulas. All you need to do is remember the cases and their specific approach, and you can manipulate hex strings easily.