0

Got a problem with encoding process.

 def str2bin(message):
 binary = bin(int(binascii.hexlify(message), 16))
 return binary[2:]

The error is :

binary = bin(int(binascii.hexlify(message), 16)) TypeError: a bytes-like object is required, not 'str'

I'm trying to type only ascii in a program. What is causing the error?

SeeDerekEngineer
7802 gold badges7 silver badges23 bronze badges
asked Jul 19, 2017 at 14:27
1
  • 2
    It's not clear what your title has to do with your question. Did you mean "steganography" rather than "stenography"? (Even then, it's still not clear.) Commented Jul 19, 2017 at 14:31

1 Answer 1

2

You need to encode the string, either in your function or before you pass it to your function:

import binascii
def str2bin(message):
 binary = bin(int(binascii.hexlify(message.encode("ascii")), 16))
 return binary[2:]
print(str2bin("X")) # 1011000

The reason is, that hexlify expects a data type that supports the buffer interface.

A bytes-like object does, a str does not.

See also the note on the binascii docs:

a2b_* functions accept Unicode strings containing only ASCII characters. Other functions only accept bytes-like objects (such as bytes, bytearray and other objects that support the buffer protocol).

answered Jul 19, 2017 at 14:29
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.