197

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

4
  • 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. Commented Feb 9, 2012 at 12:10
  • 1
    @SvenMarnach i use this perl hex() manual 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" Commented Feb 9, 2012 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. Commented Feb 9, 2012 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) stackoverflow.com/questions/209513/… Commented Oct 10, 2019 at 11:26

3 Answers 3

355

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.

Sign up to request clarification or add additional context in comments.

8 Comments

For a negative hex number, this don't work.
@EricWang This works perfectly fine for negative numbers: 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.)
hex -> decimal -> binary -> decimal: int(bin(int(hex(42), 16)), 2)
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.
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.
|
60
>>> int("0xff", 16)
255

or

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

Read the docs.

2 Comments

int(0xff) will do the right thing. It only requires an explicit base if it's passed as a string.
@cassm and in fact, there's no point in int() in that case - it is like writing int(255) - you can just write 0xff
24

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.