How can I convert bin to char and char to bin in ASCII (in byte)?
For example if I have :
1010111001100111110010101001011111000001101111011000011
I should have ®Ï*¾°
but if I convert this char to bin I will have:
1010111011001111101010101111101101110110000
This binary string is not the same, because for example the char * is obtained with 00101010
when I convert bin to char. But, when I convert the char * to bin I have 101010
.
Here is my code:
def bin_to_char(self,text_bin):
char=''
stock=''
for bit in text_bin:
if len(stock)<8:
stock+=bit
elif len(stock)==8:
print(stock)
char+=chr(int(stock, 2))
print(char)
stock=''
char+=chr(int(stock, 2)) #add the last binary text less than 8
return(char)
def char_to_bin(self,char):
chbin=''
for e in char:
print(e)
chbin+=format(ord(e), 'b')
print(chbin)
return(chbin)