"convert string to hex in python" Code Answer


your class.function expects an integer which can be represented either by a decimal or a hexadecimal literal, so that these two calls are completely equivalent:

class.function(119)  # 0x77 == 119

even print(0x77) will show 119 (because decimal is the default representation).

so, we should rather be talking about converting a string representation to integer. the string can be a hexadecimal representation, like '0x77', then parse it with the base parameter:

 >>> int('0x77', 16)

or a decimal one, then parse it as int('119').

still, storing integer whenever you deal with integers is better.

edit: as @gnibbler suggested, you can parse as int(x, 0), which handles both formats.

By pingsft on April 13 2022

Answers related to “convert string to hex in python”

Only authorized users can answer the Search term. Please sign in first, or register a free account.