1

I am writing some code:

image = Image.open(filename)    #opening the image
binary = bin(int(binascii.hexlify(message), 16))    #converting string into binary
binary = binary.append('1111111111111110')   #adding delimeter at the end 
binary = binary.lstrip('0b')   #removing the starting ob of binary

Because of line 2, I am getting this error:

binary = bin(int(binascii.hexlify(message), 16))

TypeError: a bytes-like object is required, not 'str'

I don’t understand this error and nothing seems to work.

1
  • Where is message assigned? hexlify expects bytes, not a string. Commented Feb 19, 2021 at 5:41

1 Answer 1

3

The variable message contains a string, but it needs to be a sequence of bytes. To encode a string into bytes, you can do something like this:

message.encode('utf-8')
Sign up to request clarification or add additional context in comments.

4 Comments

encode function in python converts a string into hexadecimal (unicode) then should i remove binascii.hexlify because hexlify will also convert into string into hexadecimal?
Depends what you're trying to accomplish. From the perspective of function signatures, hexlify returns a value that is the same type as its parameter (they're both bytes), so you could get rid of it. My guess is that the encode method is what you actually want and you can get rid of hexlify, but I don't know what the end goal is.
Happy to help! Please mark my answer as accepted so people in the future know that it worked :)
sure that would be a great help for other beginners like me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.