Converting to Hex in Python [duplicate] - python

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

Related

How ~12 gives -13 in decimal? [duplicate]

This question already has answers here:
What is “two's complement”?
(24 answers)
How to think about Python's negative number bitwise operations?
(2 answers)
Closed 1 year ago.
I have a question in my programming class, it is: What is the value of ~12 and the answer is -13. I don't understand why? I convert 13 base 10 to binary, which is 1100, than I switched all the 1 for 0 and vice versa, it gives me 0011, so I thought the answer was 3 but it's not.
Converting 12 to a signed binary number yields 01100 then assuming that ~ is supposed to invert every bit we get 10011 which can be converted from signed binary to decimal yielding -13.

how to do division in python without decimal points [duplicate]

This question already has an answer here:
Python 3 integer division [duplicate]
(1 answer)
Closed 2 years ago.
if I use integer type cast conversion technique then it doesn't work for large numbers like 12630717197566440063
I got wrong answer in some cases like below in python 3
a =12630717197566440063;
print(a)
temp = a/10
print(int(temp))
Then I am getting 1263071719756644096 as a answer instead of 1263071719756644006
You can use the // (floor division) operator:
temp = a//10
print(temp)

Convert binary back to ascii in python [duplicate]

This question already has answers here:
Convert binary to ASCII and vice versa
(8 answers)
Closed 4 years ago.
So i have a function that converts an alphabet character to it's binary.
def toBinary(char):
return "".join([format(ord(char), '#010b')[2:]])
For example, toBinary('a') gives me
01100001
How do i convert 01100001 back to the ascii 'a'?
One way could be
c = chr(int(s, 2))
where s is the binary string.
Try This:
chr(int('01100001',2))

Python Binary string to ASCII text [duplicate]

This question already has answers here:
Convert binary to ASCII and vice versa
(8 answers)
Closed 6 years ago.
x = "01100001"
How do i convert this string into ASCII
Expected Result:
print(x.somefunction())
Output: a
Use following one liner
x = chr(int('010101010',2))
will work.

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