Python how to put a 32 bit integer in a byte array

I usually do such things in C ++, but I use python to write a quick script, and I ran into a wall.

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

I need to change the bytes [8-11] to save the new 32-bit file size (read: there are already files there, I need to update it). In C ++, I would just get a pointer to a location and drop it to store an integer, but with python, I suddenly realized that I had no idea how to do this.

How can I update 4 bytes in my buffer in a specific place to store an integer value in python?

EDIT

I am going to add more because I cannot figure it out from the solutions (although I can see that they are on the right track).

First of all, I'm on python 2.4 (and cannot upgrade the servers of large corporations) - so that seems to be limiting my options. Sorry I didn’t mention this before, I didn’t know that it had so many functions.

Secondly, let's make it super simple.

Suppose I have a binary file named 'myfile.binary' with five-byte contents “4C53535353” in hexadecimal format - this is equivalent to the ascii representation for the letters “L and 4xS” in the file.

If I do this:

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

the content should (from Sven Marnach’s answer) contain a five-byte immutable string.

Python 2.4, 4 S "" ? , [1-4], 32- "myint" 12345678910.

+5
5

:

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

http://docs.python.org/library/struct.html .

:

import struct
import ctypes

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

: Python: struct.pack_into

EDIT: Python 2.4:

import struct

f=open('myfile.binary', 'rb')
contents=f.read(5)
data=list(contents)
data[0:4]=struct.pack(">i", 0x12345678)
print data
+8

Struct . pack.

EDIT:

:

import struct

s = "LSSSS" # your string
s = s[0] + struct.pack('<I', 1234567891) # note "shorter" constant than in your example
print s

:

L╙☻I

struct.pack Python2.4.

"12345678910" 4 , .

+4

file.read() - Python, . , , .

array module: 32- . .

with open("filename") as f:
    f.seek(0, 2)
    size = f.tell()
    f.seek(0)
    data = array.array("i")
    assert data.itemsize == 4
    data.fromfile(f, size // 4)
data[2] = new_value
# use data.tofile(g) to write the data back to a new file g
+2

numpy, .

read_data = numpy.fromfile(file = id, dtype = numpy.uint32)

.

+2

The following is just a demonstration of what actually happens when four bytes are converted to an integer. Suppose you have a number: 15213

Decimal: 15213
Binary: 0011 1011 0110 1101
Hex: 3 B 6 D

On small-end systems (e.g. x86 machines) this number can be represented using length-4 bytearray as: b"\x6d\x3b\x00\x00"or b"m;\x00\x00"when printed on the screen to convert four bytes to an integer, we just do a little basic conversion, which in this case:

sum(n*(256**i) for i,n in enumerate(b"\x6d\x3b\x00\x00"))

This gives you the result: 15213

0
source

All Articles