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 9097591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:09:14+00:00 2026-06-17T00:09:14+00:00

I have a binary file with a known format/structure. How do I read all

  • 0

I have a binary file with a known format/structure.

How do I read all the binary data in to an array of the structure?

Something like (in pseudo code)

bytes = read_file(filename)
struct = {'int','int','float','byte[255]'}
data = read_as_struct(bytes, struct)
data[1]
>>> 10,11,10.1,Arr[255]

My solution so far is:

data = []

fmt   = '=iiiii256i'
fmt_s = '=iiiii'
fmt_spec = '256i'

struct_size = struct.calcsize(fmt)

for i in range(struct_size, len(bytes)-struct_size, struct_size):
    dat1= list(struct.unpack(fmt_s, bytes[i-struct_size:i-1024]))
    dat2= list(struct.unpack(fmt_spec, bytes[i-1024:i]))
    dat1.append(dat2)
    data.append(dat1)
  • 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-06-17T00:09:15+00:00Added an answer on June 17, 2026 at 12:09 am

    Use the struct module; you need to define the types in a string format documented with that library:

    struct.unpack('=HHf255s', bytes)
    

    The above example expects native byte-order, two unsigned shorts, a float and a string of 255 characters.

    To loop over an already fully read bytes string, I’d use itertools; there is a handy grouper recipe that I’ve adapted here:

    from itertools import izip_longest, imap
    from struct import unpack, calcsize
    
    fmt_s = '=5i'
    fmt_spec = '=256i'
    size_s = calcsize(fmt_s)
    size = size_s + calcsize(fmt_spec)
    
    def chunked(iterable, n, fillvalue=''):
        args = [iter(iterable)] * n
        return imap(''.join, izip_longest(*args, fillvalue=fillvalue))
    
    data = [unpack(fmt_s, section[:size_s]) + (unpack(fmt_spec, section[size_s:]),)
        for section in chunked(bytes, size)]
        
    

    This produces tuples rather than lists, but it’s easy enough to adjust if you have to:

    data = [list(unpack(fmt_s, section[:size_s])) + [list(unpack(fmt_spec, section[size_s:]))]
        for section in chunked(bytes, size)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a binary file that I would like to regex search/replace hex bytes
I have one binary file and I want to read this file like first
I have a binary file I wish to read for my android app. the
I need to read a large (2000x2000) matrix of binary data from a file
I have a binary file of unknown format that I need to be able
I have a binary file in big-endian format from which I am retrieving 2-bit
I have 3000 binary files (each of size 40[MB]) of known format (5,000,000 'records'
I'm writing a binary data format to file containing a graph of serialized objects.
I wanna know how it is possible to read a file in binary format.
I have a binary file that contains blocks of information ( I'll refer to

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.