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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:32:53+00:00 2026-06-09T05:32:53+00:00

I am trying to use struct.unpack() to take apart a data record that ends

  • 0

I am trying to use struct.unpack() to take apart a data record that ends with an ASCII string.

The record (it happens to be a TomTom ov2 record) has this format (stored little-endian):

  • 1 byte
  • 4 byte int for total record size (including this field)
  • 4 byte int
  • 4 byte int
  • variable-length string, null-terminated

unpack() requires that the string’s length be included in the format you pass it. I can use the second field and the known size of the rest of the record — 13 bytes — to get the string length:

str_len = struct.unpack("<xi", record[:5])[0] - 13
fmt = "<biii{0}s".format(str_len)

then proceed with the full unpacking, but since the string is null-terminated, I really wish unpack() would do it for me. It’d also be nice to have this should I run across a struct that doesn’t include its own size.

How can I make that happen?

  • 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-09T05:32:56+00:00Added an answer on June 9, 2026 at 5:32 am

    The size-less record is fairly easy to handle, actually, since struct.calcsize() will tell you the length it expects. You can use that and the actual length of the data to construct a new format string for unpack() that includes the correct string length.

    This function is just a wrapper for unpack(), allowing a new format character in the last position that will drop the terminal NUL:

    import struct
    def unpack_with_final_asciiz(fmt, dat):
        """
        Unpack binary data, handling a null-terminated string at the end 
        (and only at the end) automatically.
    
        The first argument, fmt, is a struct.unpack() format string with the 
        following modfications:
        If fmt's last character is 'z', the returned string will drop the NUL.
        If it is 's' with no length, the string including NUL will be returned.
        If it is 's' with a length, behavior is identical to normal unpack().
        """
        # Just pass on if no special behavior is required
        if fmt[-1] not in ('z', 's') or (fmt[-1] == 's' and fmt[-2].isdigit()):
            return struct.unpack(fmt, dat)
    
        # Use format string to get size of contained string and rest of record
        non_str_len = struct.calcsize(fmt[:-1])
        str_len = len(dat) - non_str_len
    
        # Set up new format string
        # If passed 'z', treat terminating NUL as a "pad byte"
        if fmt[-1] == 'z':
            str_fmt = "{0}sx".format(str_len - 1)
        else:
            str_fmt = "{0}s".format(str_len)
        new_fmt = fmt[:-1] + str_fmt
    
        return struct.unpack(new_fmt, dat)
    

    >>> dat = b'\x02\x1e\x00\x00\x00z\x8eJ\x00\xb1\x7f\x03\x00Down by the river\x00'
    >>> unpack_with_final_asciiz("<biiiz", dat)
    (2, 30, 4886138, 229297, b'Down by the river')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to use a struct to parse socket data when implement a
I have a struct that I am trying to use like an enum: public
I am trying to use the pt_regs struct to get and set registers such
Hi I am trying to use C to implement a simple struct: 2 boxes,
While trying to use XMLWorkerHelper.GetInstance().ParseXHTML() i find that it is really strict. Any wrong
I'm trying to use the memory allocated beyond the size of a struct to
I am trying to use qsort from STL to sort array of edge: struct
Introduction I am trying to use P/Invoke to register a struct of callbacks with
I'm trying to use struct to map header of a BitMap file. It seems
I'm trying to use memset on a struct element like so: memset( &targs[i]->cs, 0,

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.