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))
Related
This question already has answers here:
Convert string (in scientific notation) to float
(4 answers)
Closed 9 months ago.
How can I parse a character string that represents a scientific number into a numeric number?
>>> float('5e-04')
0.0005
This question already has answers here:
Converting integer to binary in python
(17 answers)
Closed 3 years ago.
I have a string of 6 characters (only 0s and 1s)on which I have to use binary operations. I came across a few pages but most mentioned converting characters to binary and appending them to give the final result.
I have to use it in this function
def acmTeam(topic):
global finalCount
for i in range(len(topic)-1):
for j in range(i+1,len(topic)):
final=toBinary(topic[i])|toBinary(topic[j])
print(final)
One example value of topic is
['10101', '11100', '11010', '00101']
and i want 10101 and 11100 binary values
Here I can create my own function toBinary to convert it to binary equivalent and return, but is there any inbuilt function or more efficient way to do so in python?
Thanks in advance :)
Try this:
int(s, base=2)
for example,
for i in ['10101', '11100', '11010', '00101']:
print(int(i, base=2))
#metatoaster also mentioned this.
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.
This question already has answers here:
Print a string as hexadecimal bytes
(13 answers)
Closed 7 years ago.
I want to convert string to a hex (decimal),
for example abc to 616263 and so on.
How do I do that?
I have tried:
x= input("Enter string")
x = int(x)
hex(x)
But it is not working.
maybe you are looking for something like:
x.encode("hex")
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.