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

  • Home
  • SEARCH
  • 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 6736227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:06:19+00:00 2026-05-26T11:06:19+00:00

I usually perform things like this in C++, but I’m using python to write

  • 0

I usually perform things like this in C++, but I’m using python to write a quick script and I’ve run into a wall.

If I have a binary list (or whatever python stores the result of an “fread” in). I can access the individual bytes in it with: buffer[0], buffer[1], etc.

I need to change the bytes [8-11] to hold a new 32-bit file-size (read: there’s already a filesize there, I need to update it). In C++ I would just get a pointer to the location and cast it to store the integer, but with python I suddenly realized I have no idea how to do something like this.

How can I update 4 bytes in my buffer at a specific location to hold the value of an integer in python?

EDIT

I’m going to add more because I can’t seem to figure it out from the solutions (though I can see they’re on the right track).

First of all, I’m on python 2.4 (and can’t upgrade, big corporation servers) – so that apparently limits my options. Sorry for not mentioning that earlier, I wasn’t aware it had so many less features.

Secondly, let’s make this ultra-simple.

Lets say I have a binary file named ‘myfile.binary’ with the five-byte contents ‘4C53535353’ in hex – this equates to the ascii representations for letters “L and 4xS” being alone in the file.

If I do:

f = open('myfile.binary', 'rb')
contents = f.read(5)

contents should (from Sven Marnach’s answer) hold a five-byte immutable string.

Using Python 2.4 facilities only, how could I change the 4 S’s held in ‘contents’ to an arbitrary integer value? I.e. give me a line of code that can make byte indices contents [1-4] contain the 32-bit integer ‘myint’ with value 12345678910.

  • 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-26T11:06:19+00:00Added an answer on May 26, 2026 at 11:06 am

    What you need is this function:

    struct.pack_into(fmt, buffer, offset, v1, v2, ...)
    

    It’s documented at http://docs.python.org/library/struct.html near the top.

    Example code:

    import struct
    import ctypes
    
    data=ctypes.create_string_buffer(10)
    struct.pack_into(">i", data, 5, 0x12345678)
    print list(data)
    

    Similar posting: Python: How to pack different types of data into a string buffer using struct.pack_into

    EDIT: Added a Python 2.4 compatible example:

    import struct
    
    f=open('myfile.binary', 'rb')
    contents=f.read(5)
    data=list(contents)
    data[0:4]=struct.pack(">i", 0x12345678)
    print data
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After creating a new form, I usually perform this ritual: Change the name into
Usually when I need to fork in C, I do something like this: pid_t
Usually the compiler generates code to perform boxing and unboxing. But what does the
usually selenium does not identify elements on a browser toolbar. But this is for
Usually Flash and Flex applications are embedded on in HTML using either a combination
Usually when I build a site, I put all the CSS into one file,
Usually, when using Windows Authentication, the software (eg Internet Explorer) tries to use Windows
Usually I tend to install things via the package manager, for unixy stuff. However,
I want to perform some action when a link is clicked, I am using
When I write math in LaTeX I often need to perform simple arithmetic on

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.