Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, how to put 32-bit integer into byte array

Tags:

python

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.

like image 368
John Humphreys Avatar asked Oct 27 '11 20:10

John Humphreys


People also ask

How do I convert an integer to a byte array?

When you want to convert an int value to a byte array, you can use the static method ByteArray. toByteArray(). This method takes an int input and returns a byte array representation of the number.

How do you convert int to byte in Python?

An int value can be converted into bytes by using the method int. to_bytes(). The method is invoked on an int value, is not supported by Python 2 (requires minimum Python3) for execution.

How do I get 32 bit integer in Python?

The int data type in python simply the same as the signed integer. A signed integer is a 32-bit integer in the range of -(2^31) = -2147483648 to (2^31) – 1=2147483647 which contains positive or negative numbers.


2 Answers

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
like image 117
hochl Avatar answered Oct 28 '22 05:10

hochl


Have a look at Struct module. You need pack function.

EDIT:

The code:

import struct

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

Output:

L╙☻ЦI

struct.pack should be available in Python2.4.

Your number "12345678910" cannot be packed into 4 bytes, I shortened it a bit.

like image 27
Andriy Tylychko Avatar answered Oct 28 '22 04:10

Andriy Tylychko