How to convert binary value to string.

This is the place for queries that don't fit in any of the other categories.

How to convert binary value to string.

Postby psynt555 » Mon Jun 09, 2014 2:02 pm

Hi, so I've read in a file using:
Code: Select all
with open(sys.argv[1], 'rb') as f:
    contents =

How do I convert this whole binary value to a string? I need this because I am taking a file (PDF for example) and wanting to check what PDF version it is by examining the header.
Posts: 1
Joined: Mon Jun 09, 2014 1:58 pm

Re: How to convert binary value to string.

Postby stranac » Mon Jun 09, 2014 2:58 pm

If you are using python 2, contents already is a string.
In python 3, it is a bytes object, which should allow you to do any checking you want.

Do you have an actual problem? You should show us your failing code (and full error traceback, if you get one.)
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 6 guests