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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:08:05+00:00 2026-06-06T20:08:05+00:00

i have a binary file which has 4 KB of header information and then

  • 0

i have a binary file which has 4 KB of header information and then 28 bytes of data and then 24 byte which i want to read. How can i loop every 24 and 28 bytes and read(or extract) every first 8 bytes of data of those 28 and 24 bytes.. In python i did something like this. Not sure how to do for variable length

import sys
import struct
f = open(sys.argv[1],"rb")
f.seek(4096)
byte = f.read(28)
while byte != "":   
    ticks = struct.unpack("<ll",byte[:8]) #not sure how to read 8 bytes 
    byte = f.read(28)
f.close()

Here is what it looks like after the header.

Length
(bytes) Field Name
8   TS_INCR
4   SEQID
2   OP
2   LUN
4   NBLKS
8   LBA


Length
(bytes) Field Name
8   TS_INCR
4   SEQID
2   OP
2   LUN
4   LATENCY_TICKS
2   HOST_ID
2   HOST_LUN

If you guys can help with this please. Python or PERL does not matter. 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-06-06T20:08:07+00:00Added an answer on June 6, 2026 at 8:08 pm

    The Endianness of the data you are reading matters here. You seem to be unpacking the 8 octets as two longs stored in little endian order. Are you sure it is not a single 64-bit quantity (which would make the q or Q formats more appropriate)? Unfortunately, I am on 32-bit machine so my perl does not support Q.

    However, the following should point you in the right direction:

    #!/usr/bin/env perl
    
    use strict; use warnings;
    use autodie;
    
    use Fcntl qw(:seek);
    use List::Util qw( sum );
    
    my ($input_file) = @ARGV;
    die "Need input file\n" unless defined $input_file;
    
    my $HEADER_SIZE = 4_096;
    
    my @typedef = (
        {
            fields => [
                qw(
                    TS_INCR_LO
                    TS_INCR_HI
                    SEQID
                    OP
                    LUN
                    NBLKS
                    LBA_LO
                    LBA_HI
                )
            ],
            tmpl => 'LLLSSLLL',
            start => 0,
            size => 28,
        },
        {
            fields => [
                qw(
                    TS_INCR_LO
                    TS_INCR_HI
                    SEQID
                    OP
                    LUN
                    LATENCY_TICKS
                    HOST_ID
                    HOST_LUN
                )
            ],
            tmpl => 'LLLSSLSS',
            start => 28,
            size => 24,
        },
    );
    
    open my $input, '<:raw', $input_file;
    
    seek $input, $HEADER_SIZE, SEEK_SET;
    
    my $BLOCK_SIZE = sum map $_->{size}, @typedef;
    read $input, my($buffer), $BLOCK_SIZE;
    
    my @structs;
    
    for my $t ( @typedef ) {
        my %struct;
        @struct{ @{ $t->{fields}} } = unpack(
            $t->{tmpl},
            substr($buffer, $t->{start}, $t->{size})
        );
        push @structs, \%struct;
    }
    
    use Data::Dumper;
    print Dumper \@structs;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some binary data which has a corresponding map file which identifies each
i want to read\write a binary file which has the following structure: The file
I have a binary file which has an ASCII metadata header. The elements are
i have one file (name.dat) which is binary data, it has 12 lines of
I have a binary file to which I want to append a chunk of
I have a binary file, inside of which has multiple frames. Each frame starts
I have a binary file (test.bin) which has 2 unsigned int values respectively 1000
I have one binary file which I have created. In it, data is stored
I have a binary file of size about 5MB.. which has lots of interspersed
I have a binary file to which I'm trying to write however I dont

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.