178

I have some Perl code where the hex() function converts hex data to decimal. How can I do it on Python?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sir D
  • 2,164
  • 2
  • 17
  • 21
  • 1
    Are you sure you want to convert the data to "decimal"? The Perl function simply converts a hex string to an integer, not to a decimal string. – Sven Marnach Feb 09 '12 at 12:10
  • @SvenMarnach i use this perl hex() manual http://www.misc-perl-info.com/perl-hex.html "This function: has as argument a hexadecimal string (or an expression which after evaluation will return a hex string) will return the decimal corresponding value" – Sir D Feb 09 '12 at 12:18
  • 2
    @Sir D: that article is very badly phrased. As Sven says, `hex` converts a hex string to an integer value (which is stored internally in binary but is essentially simply a numeric value without a base). The call to `print` converts that value into a decimal string and prints it. – Borodin Feb 09 '12 at 12:32
  • Extended question "How to convert from hexadecimal OR decimal (OR binary) to integer as python interpreter does." is answered in the "already has answer here question". int(string,base) function infer the base of input string from string prefix If you pass 0 as the base. e.g. int("0x1402",0) int("0b1010000000010",0) int("5122",0) https://stackoverflow.com/questions/209513/convert-hex-string-to-int-in-python – gaoithe Oct 10 '19 at 11:26

3 Answers3

330

If by "hex data" you mean a string of the form

s = "6a48f82d8e828ce82b82"

you can use

i = int(s, 16)

to convert it to an integer and

str(i)

to convert it to a decimal string.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • 1
    For a negative hex number, this don't work. – Eric May 20 '16 at 04:21
  • 2
    @EricWang This works perfectly fine for negative numbers: http://ideone.com/IHOQvp. Unless you have a negative number encoded in fixed-width two's complement format, but that would be a different problem than what was asked here. (Hint: use the `struct` module in that case.) – Sven Marnach May 20 '16 at 11:32
  • @SvenMarnach Yes, I mean 2'complement used in elf64 format. – Eric May 20 '16 at 13:27
  • 2
    hex -> decimal -> binary -> decimal: `int(bin(int(hex(42), 16)), 2)` – arcolife Jan 24 '18 at 06:55
  • 3
    Beware that this is a **big endian** interpretation. That would be suitable for some things (many network protocols for example are big endian) but not for others, like looking at *internal* program data from the current majority of systems which are natively little endian. – Chris Stratton Jan 03 '19 at 19:09
  • @ChrisStratton While your comment is sort of accurate, it is also misleading. You typically will have binary data in the applications you mention, nott strings containing a hexadecimal number. You _could_ convert that binary data to a hex string, and then convert it to a decimal number, but there is little point in doing so. Using the `struct` module to convert the binary data directly seems more appropriate. – Sven Marnach Jan 03 '19 at 21:25
  • 1
    On the contrary, it is actually quite common to encounter data that has *already* been rendered as human readable hex, which contains little endian numeric values. So no, the comment is not misleading in the slightest. Python gets used not only in comprehensive all-python solutions, but very heavily for processing the output and logs of other systems and gluing test rigs together. – Chris Stratton Jan 03 '19 at 21:28
  • @ChrisStratton Fair enough. Even then I'd argue you are better off by first parsing the textual representation into a binary representation (e.g. using the `binascii` module) and then interpreting the binary data, but I guess it depends on the use case. – Sven Marnach Jan 03 '19 at 21:36
56
>>> int("0xff", 16)
255

or

>>> int("FFFF", 16)
65535

Read the docs.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • 4
    int(0xff) will do the right thing. It only requires an explicit base if it's passed as a string. – cassm Jan 15 '16 at 17:11
  • 1
    @cassm and in fact, there's no point in `int()` in that case - it is like writing `int(255)` - you can just write `0xff` – jbg Feb 22 '19 at 10:41
21

You could use a literal eval:

>>> ast.literal_eval('0xdeadbeef')
3735928559

Or just specify the base as argument to int:

>>> int('deadbeef', 16)
3735928559

A trick that is not well known, if you specify the base 0 to int, then Python will attempt to determine the base from the string prefix:

>>> int("0xff", 0)
255
>>> int("0o644", 0)
420
>>> int("0b100", 0)
4
>>> int("100", 0)
100
wim
  • 338,267
  • 99
  • 616
  • 750