Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

http://docs.python.org/library/struct.html[^]

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 :

http://stackoverflow.com/questions/1550721/python-struct-pack-equivalent-in-c[^]
 
Share this answer
 
Comments
pasztorpisti 8-Sep-12 14:05pm    
+5
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:
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: http://docs.python.org/library/struct.html#struct-alignment[^]

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:

'>LL':
C++
unsigned long[2];

'>256L':
C++
unsigned long[256];

'>64L':
C++
unsigned long[64];
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900