How do I convert hex to decimal in Python? [duplicate] - python

This question already has answers here:
Convert hex string to integer in Python
(10 answers)
Closed 8 years ago.
I have some Perl code where the hex() function converts hex data to decimal. How can I do it on Python?

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.

>>> int("0xff", 16)
255
or
>>> int("FFFF", 16)
65535
Read the docs.

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

Related

How to convert from bytes to int? [duplicate]

This question already has answers here:
How to convert a string of bytes into an int?
(12 answers)
Closed 4 years ago.
I know that to convert from int to bytes you have to do this, for example:
>>>a = bytes(4)
>>>print(a)
b'\x00\x00\x00\x00'
But what do I do if I want to revert it and convert bytes to int or float?
I tried using:
int.from_bytes( a, byteorder='little')
and
int.from_bytes( a, byteorder='big', signed=True)
but it did not work.
import struct
val = struct.unpack( '<I', b'\x00\x00\x00\x00')[0]
or something along the lines... control the big/little with < or > sign.
Here are the docs: 7.1. struct — Interpret bytes as packed binary data

Converting to Hex in Python [duplicate]

This question already has answers here:
Convert hex string to integer in Python
(10 answers)
Closed 8 years ago.
I know this may be an incredibly easy answer but I couldn't find the answer to this so is there an easy way to convert between hex and decimal and binary and hex in python like the way you can convert from decimal to binary with int()?
Thanks in advance for your answers. - Ed
Yes, it's quite easy:
>>> hex(33)
'0x21'
>>> oct(33)
'041'
>>> bin(33)
'0b100001'
>>> int('0x21', 16)
33
>>> int('041', 8)
33
>>> int('0b100001', 2)
33

converting a string to binary number in python [duplicate]

This question already has answers here:
Convert base-2 binary number string to int
(10 answers)
Closed 9 years ago.
I want to convert a string consists of binary digits and convert that string into a binary number.
suppose, my input is "01001", this is in string format, i want to convert this to binary number format to perform various bit wise operations on that.
You can convert the number to an int and do bitwise operations, like this
my_int = int("01001", 2)
print my_int & 1 # 1
print my_int & 8 # 8
print my_int & 16 # 0

In Python, how to specify a format when converting int to string? [duplicate]

This question already has answers here:
Display number with leading zeros [duplicate]
(19 answers)
Closed 6 months ago.
In Python, how do I specify a format when converting int to string?
More precisely, I want my format to add leading zeros to have a string
with constant length. For example, if the constant length is set to 4:
1 would be converted into "0001"
12 would be converted into "0012"
165 would be converted into "0165"
I have no constraint on the behaviour when the integer is greater than what can allow the given length (9999 in my example).
How can I do that in Python?
"%04d" where the 4 is the constant length will do what you described.
You can read about string formatting here.
Update for Python 3:
{:04d} is the equivalent for strings using the str.format method or format builtin function. See the format specification mini-language documentation.
You could use the zfill function of str class. Like so -
>>> str(165).zfill(4)
'0165'
One could also do %04d etc. like the others have suggested. But I thought this is more pythonic way of doing this...
With python3 format and the new 3.6 f"" notation:
>>> i = 5
>>> "{:4n}".format(i)
' 5'
>>> "{:04n}".format(i)
'0005'
>>> f"{i:4n}"
' 5'
>>> f"{i:04n}"
'0005'
Try formatted string printing:
print "%04d" % 1 Outputs 0001
Use the percentage (%) operator:
>>> number = 1
>>> print("%04d") % number
0001
>>> number = 342
>>> print("%04d") % number
0342
Documentation is over here
The advantage in using % instead of zfill() is that you parse values into a string in a more legible way:
>>> number = 99
>>> print("My number is %04d to which I can add 1 and get %04d") % (number, number+1)
My number is 0099 to which I can add 1 and get 0100

convert string rep of hexadecimal into actual hexadecimal [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
how to parse hex or decimal int in Python
i have a bunch of Hexadecimal colors in a database stored as strings.
e.g. '0xFFFF00'
when i get them from the database i need to convert this string into an actual hexadecimal number, so
0xFFFF00
how can i do this in python
This is one way to do it:
>>> s = '0xFFFF00'
>>> i = int(s, 16)
>>> print i
hex(int('0xFFFF00', 16))
Also this works
number = int('0xFFFF00',0)
print("%x follows %x" % (number+1, number))
0 argument tells interpreter to follow the Python rules of numbers to decide the used format of number so this expression will work right for all numbers.

Categories