Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7499109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:42:16+00:00 2026-05-29T19:42:16+00:00

I have recently taken up the activity of parsing binary data with Python but

  • 0

I have recently taken up the activity of parsing binary data with Python but am confused by the way “byte” items are treated by Python. Take for e.g. the following interpreter conversation:

>>> f = open('somefile.gz', 'rb')
>>> f
<open file 'textfile.gz', mode 'rb' at 0xb77f4d88>
>>> bytes = f.read()
>>> bytes[0]
'\x1f'
>>> len(bytes[0])
1
>>> int(bytes[0])  <---- calling __str__ automatically on bytes[0] ?
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '\x1f'

The above session shows that bytes[0] has the size of 1 byte but the __str__ representation is a hexadecimal one. No worries, but when I try to treat bytes[0] as a single byte, I get funky behaviour.

If I want to parse/interpret a binary stream based on some specification where the specification includes representation in hexadecimal, binary and decimal, how would I go about doing that.

An e.g. would be “first two bytes are \xbeef, the next is a decimal 8 followed by a packed bit field where each of the 8 bits of the byte represent some flag? I guess there are a few modules out there which make this task easy but I’d want to do it from scratch.

I have seen references to struct module but is there no way of checking the bytes read directly without introducing a new module? Something like bytes[0] == 0xbeef ?

Can someone please help me out with how normally folks parse binary data conforming a specification using Python? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:42:18+00:00Added an answer on May 29, 2026 at 7:42 pm

    You’re using Python 2.x. Prior to Python 3.0, reading a file, even a binary file, returns a string. What you’re calling a “bytes” object is really a string. Indexing into a string as you do with “bytes[0]” just returns a 1-character string.

    The struct module would probably be best suited to what you want, but you can do what you ask without it if you really want to:

    “Something like bytes[0] == 0xbeef ?”

    This won’t work because 0xbeef is a two-byte sequence, but bytes[0] is only a single byte. You can do this instead:

    bytes[0:2] == b'\xbe\xef'
    

    In Python 3.x, things work a little bit more like you’d expect. Reading a binary file returns a bytes object that behaves like a sequence of 1-byte unsigned integers, not as a string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently run across these terms few times but I am quite confused
I have recently taken on a project in which I need to integrate with
Recently, many programmers and that includes me, have taken the X out of AJAX,
I have recently taken over an application that uses the Peter Blum Date and
I've recently taken the plunge into DirectX and have been messing around a little
I have been using php for a while but haven't taken the time to
I have recently taken on a php web developer position for a large firm.
I recently have taken the support and programming of a web system written in
I have recently built a list view which shows a number of products taken
I have recently taken the decision of changing a vector of pointers into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.