Click here to Skip to main content
13,139,254 members (90,513 online)
Rate this:
Please Sign up or sign in to vote.
See more:
is there any c or c++ function similar to struct.unpact_from()?
is it possible?
or at least tell me what is the difference between these three statements???
struct.unpack_from('>LL', f, 30)
struct.unpack_from('>256L', f, 60)
struct.unpack_from('>64L', f, 90)
Posted 8-Sep-12 2:56am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1[^]

explains this and other functions.
They are quite specific to python, nothing like this is natively supported in C or C++.

There is a library however that you can have a look at see also here :[^]
pasztorpisti 8-Sep-12 14:05pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

The struct library in python is intended to mimic the binary serialization of "low level" languages like C/C++, you don't have to emulate the python struct library in C/C++ if you use fixed predefined data structures! The 3 strings - '>LL' and its friends - can be translated to their respective C struct equivalent. All you have to deal with in C/C++ as an extra is endianness if your program has to run on different architectures.
For example '>LL' is like this in C:
#pragma pack(push, 1)
struct {
    unsigned long member1;
    unsigned long member2;
#pragma pack(pop)

Since the '>' character in the '>LL' format string means that the data is always big endian, you have to swap the byte order of every integers in your struct when you serialize or deserialize on a little endian architecture. Philip Stuyck linked a struct-pack-equivalent-in-c but that is rarely needed in practice. People usually never use the extra power that struct library of python has over C structures that is - you don't have to know the format of the struct at compile time. In python you could receive even the format of the struct for example over network - but in practice I myself never seen something like that. If your python program uses a fixed number of predefined struct format strings like '>LL' then you should just translate these to their equivalent C structs and handle endianness if necessary. Make sure that in your python format string you also use the same alignment as in your C code - thats why the pragma pack is there! For format string specifications see:[^]

EDIT: Some higher level languages like java and python don't have mechanics to handle structured binary data like the sturcts in C/C++, but sometimes you have to deal with binary data that was serialized by some C/C++ program - thats why the struct library exists in python. The fact that it has some more power (dynamic behavior) than C/C++ sturcts is rarely exploited and can be implemented in other much more performance friendly ways in C/C++. Go with native structs in C/C++, its unlikely that you have to use a C/C++ library that mimics that extra unneeded power of python structs by introducing complexity and performance downgrade to your program!
In your case the structs contain the same member data types so in C/C++ you could just declare an array to get the same binary structure as your python format strings:

unsigned long[2];

unsigned long[256];

unsigned long[64];

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 8 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100