Decode it.
>>> b'a string'.decode('ascii')
'a string'
To get bytes from string, encode it.
>>> 'a string'.encode('ascii')
b'a string'
Answer from falsetru on Stack OverflowString to binary
how do i convert string to binary string and the reverse too?
Binary to String/Text in Python - Stack Overflow
binary data to string
Videos
Something like this?
>>> st = "hello world"
>>> ' '.join(format(ord(x), 'b') for x in st)
'1101000 1100101 1101100 1101100 1101111 100000 1110111 1101111 1110010 1101100 1100100'
#using `bytearray`
>>> ' '.join(format(x, 'b') for x in bytearray(st, 'utf-8'))
'1101000 1100101 1101100 1101100 1101111 100000 1110111 1101111 1110010 1101100 1100100'
If by binary you mean bytes type, you can just use encode method of the string object that encodes your string as a bytes object using the passed encoding type. You just need to make sure you pass a proper encoding to encode function.
In [9]: "hello world".encode('ascii')
Out[9]: b'hello world'
In [10]: byte_obj = "hello world".encode('ascii')
In [11]: byte_obj
Out[11]: b'hello world'
In [12]: byte_obj[0]
Out[12]: 104
Otherwise, if you want them in form of zeros and ones --binary representation-- as a more pythonic way you can first convert your string to byte array then use bin function within map :
>>> st = "hello world"
>>> map(bin,bytearray(st))
['0b1101000', '0b1100101', '0b1101100', '0b1101100', '0b1101111', '0b100000', '0b1110111', '0b1101111', '0b1110010', '0b1101100', '0b1100100']
Or you can join it:
>>> ' '.join(map(bin,bytearray(st)))
'0b1101000 0b1100101 0b1101100 0b1101100 0b1101111 0b100000 0b1110111 0b1101111 0b1110010 0b1101100 0b1100100'
Note that in python3 you need to specify an encoding for bytearray function :
>>> ' '.join(map(bin,bytearray(st,'utf8')))
'0b1101000 0b1100101 0b1101100 0b1101100 0b1101111 0b100000 0b1110111 0b1101111 0b1110010 0b1101100 0b1100100'
You can also use binascii module in python 2:
>>> import binascii
>>> bin(int(binascii.hexlify(st),16))
'0b110100001100101011011000110110001101111001000000111011101101111011100100110110001100100'
hexlify return the hexadecimal representation of the binary data then you can convert to int by specifying 16 as its base then convert it to binary with bin.
I want a function that takes for example 'Hello brother! ๐'(a combination of any unicode symbols) as input and outputs the binary equivalent using utf-8 so here:
'01001000 01100101 01101100 01101100 01101111 00100000 01100010 01110010 01101111 01110100 01101000 01100101 01110010 00100001 00100000 11110000 10011111 10011000 10001010'
The output should also be a string.
is there any proper method to convert a string in utf-8 encoded formant to binary string format and the reverse too. like example
data = "this string".encode("utf-8")
data = func(data)
print(data) #want to have a string as output like "0101010101111010101010......."
It looks like you are trying to decode ASCII characters from a binary string representation (bit string) of each character.
You can take each block of eight characters (a byte), convert that to an integer, and then convert that to a character with chr():
>>> X = "0110100001101001"
>>> print(chr(int(X[:8], 2)))
h
>>> print(chr(int(X[8:], 2)))
i
Assuming that the values encoded in the string are ASCII this will give you the characters. You can generalise it like this:
def decode_binary_string(s):
return ''.join(chr(int(s[i*8:i*8+8],2)) for i in range(len(s)//8))
>>> decode_binary_string(X)
hi
If you want to keep it in the original encoding you don't need to decode any further. Usually you would convert the incoming string into a Python unicode string and that can be done like this (Python 2):
def decode_binary_string(s, encoding='UTF-8'):
byte_string = ''.join(chr(int(s[i*8:i*8+8],2)) for i in range(len(s)//8))
return byte_string.decode(encoding)
To convert bits given as a "01"-string (binary digits) into the corresponding text in Python 3:
>>> bits = "0110100001101001"
>>> n = int(bits, 2)
>>> n.to_bytes((n.bit_length() + 7) // 8, 'big').decode()
'hi'
For Python 2/3 solution, see Convert binary to ASCII and vice versa.
Hi how can i convert a binary data (read from any file) to a string which contains the exact same binary data so that i can manipulate it