Skip to content
Advertisement

How to decode binary content to original string content in python?

I have a string which I need to encode into binary. It is very important that I get a value string like ‘11010011100…’, because later I need to insert it into the lowest bit of pixels of a gray scale image. (Basically I am hiding a message inside an image.) I am on windows 10, using python 3.6.8.

Following this link I wrote this method to encode it:

def message2bin(msg):
    """
    Converts a msg to binary format
    :param msg: message to convert
    :return: binary content of message in string format
    """
    message_bin = ''.join(format(x, 'b') for x in bytearray(msg, 'utf-8'))
    return message_bin

msg = 'Hello world'
print(message2bin(msg)) 

Output is: 1001000110010111011001101100110111110000011101111101111111001011011001100100

Now I want to have a method to take this binary stream and make the original message like:

bin2message(message2bin(msg))
>> Hello world

I tried the following:

print(bytearray(message2bin(msg), 'utf-8').decode(encoding))

But the output is again:

>> 1001000110010111011001101100110111110000011101111101111111001011011001100100

Also I don’t understand why the length of the binary message is 76 and not 78? First of all it takes each character into 7 bits and not 8, which I don’t understand why. Also even with 7 bits, ‘Hello world’ has 11 characters, so I expect length 77, but it is giving me 76? Can someone please explain this to me?

print(len(message2bin(msg)))
>> 76

Thank you all for your valuable input.

Advertisement

Answer

def msgencoder(msg):
    return format(int(bytes(msg, 'utf-8').hex(), base=16), 'b')

def msgdecoder(msg):
    return bytes.fromhex(format(int(msg, base=2), 'x')).decode('utf-8')

a = msgencoder('hello world')
b = msgdecoder(a)

print(b)
hello world
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement