Checking for binary data in a string

M

Mitko Haralanov

I have a question about finding out whether a string contains binary
data?

In my application, I am reading from a file which could contain
binary data. After I have read the data, I transfer it using xmlrpclib.

However, xmlrpclib has trouble unpacking XML which contains binary data
and my application throws an exception. The solution to this problem is
to use base64 encoding of the data but I don't know how to check
whether the encoding will be needed?

If I read in a string containing some binary data from the file, the
type of that string is <type 'str'> which is not different from any
other string, so I can't use that as a check.

The only other check that I can think of is to check every character in
the read-in string against string.printable but that will take a long
time.

Can anyone suggest a better way to handle the check? Thank you in
advance.
 
L

Lie Ryan

Grant said:
All strings contain binary data.

Not quite, (python 2.x's) strings are binary data.

It just happens that it behaves like text when you appropriately
encode/decode it.

In python 3.x, the default string have evolved to unicode string, which
is a true text and the old string which contains arbitrary binary data
evolved into bytestring.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top