Python program to convert hex string to decimal Last Updated : 10 Jan, 2025 Comments Improve Suggest changes Like Article Like Report Converting a hexadecimal string to a decimal number is a common task in programming, especially when working with binary data or low-level computations. Hexadecimal values offer a concise way to represent large numbers.Using the int() FunctionUsing the int() function is the most common and straightforward method. Python hex_s = "1A3F" integer_value = int(hex_s, 16) print(f"{integer_value}") Output6719 Explanationint(hex_s, 16) converts the hexadecimal string "1A3F" to its integer value, interpreting it as base 16.The print() statement uses an f-string to display the result dynamically as Using int(): {integer_value}.Using ast.literal_eval from ast ModuleThis method can evaluate the hex string when prefixed with 0x. Python import ast hex_s = "1A3F" # Prefix the hex string with '0x' to make it evaluable integer_value = ast.literal_eval(f"0x{hex_s}") print(f"{integer_value}") Output6719 Explainationast.literal_eval(f"0x{hex_s}") safely evaluates the prefixed string "0x1A3F" as a hexadecimal number and converts it to an integer.print() statement dynamically displays the result as Using ast.literal_eval(): {integer_value}.Using struct.unpackFor fixed-width hexadecimal strings, we can convert them using struct.unpack. Python import struct hex_string = "1A3F" # Convert hex string to bytes and unpack integer_value = struct.unpack(">H", bytes.fromhex(hex_string))[0] print(f"{integer_value}") Output6719 Explanationstruct.unpack(">H", bytes.fromhex(hex_string))[0] converts the hexadecimal string "1A3F" into bytes using bytes.fromhex() and unpacks it as a big-endian 2-byte unsigned integer.The print() statement uses an f-string to display the converted integer as Using struct.unpack(): {integer_value}.Using base64.b16decodeDecode the hex string to bytes, then convert it to an integer. Python import base64 hex_string = "1A3F" # Decode using base64.b16decode decoded_bytes = base64.b16decode(hex_string) integer_value = int.from_bytes(decoded_bytes, byteorder="big") print(f"{integer_value}") Output6719 Explanationbase64.b16decode(hex_string) decodes the hexadecimal string "1A3F" into bytes.int.from_bytes(decoded_bytes, byteorder="big") converts the bytes to an integer, displayed with print(). Comment More infoAdvertise with us Next Article Python program to convert hex string to decimal M manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python Program to Convert Decimal to Hexadecimal In this article, we will learn how to convert a decimal value(base 10) to a hexadecimal value (base 16) in Python. Method 1: Using hex() function hex() function is one of the built-in functions in Python3, which is used to convert an integer number into its corresponding hexadecimal form. Syntax : 3 min read Python Program to Convert Binary to Hexadecimal Given a binary number, the task is to write a Python program to convert the given binary number into an equivalent hexadecimal number. i.e convert the number with base value 2 to base value 16. In hexadecimal representation we 16 values to represent a number. Numbers 0-9 are expressed by digits 0-9 4 min read Python program to convert int to exponential Given a number of int type, the task is to write a Python program to convert it to exponential. Examples: Input: 19 Output: 1.900000e+01 Input: 2002 Output: 2.002000e+03 Input: 110102 Output: 1.101020e+05Approach: We will first declare and initialise an integer numberThen we will use format method t 1 min read Python program to convert exponential to float Given a number in exponential format, the task is to write a Python program to convert the number from exponential format to float. The exponential number is a way of representing a number. Examples: Input: 1.900000e+01 Output: 19.0 Input: 2.002000e+03 Output: 2002.0 Input: 1.101020e+05 Output: 1101 1 min read Python program to convert float to exponential Given a float number, the task is to write a Python program to convert float to exponential. Examples: Input: 19.0 Output: 1.900000e+01 Input: 200.2 Output: 2.002000e+02 Input: 1101.02 Output: 1.101020e+03Approach: We will first declare and initialise a float number.Then we will use format method to 1 min read Like