Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C Python
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 3:56am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Comments
pasztorpisti at 8-Sep-12 14:05pm
   
+5
Rate this: bad
good
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: 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':
unsigned long[2];
'>256L':
unsigned long[256];
'>64L':
unsigned long[64];
  Permalink  
v3

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 545
1 Kornfeld Eliyahu Peter 407
2 Maciej Los 369
3 DamithSL 221
4 OriginalGriff 218
0 OriginalGriff 6,383
1 DamithSL 4,879
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,056
4 Sergey Alexandrovich Kryukov 3,927


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 8 Sep 2012
Copyright © CodeProject, 1999-2014
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