混淆在c或c ++中使用struct.unpack_from函数 [英] confused to use struct.unpack_from function in c or c++

查看:590
本文介绍了混淆在c或c ++中使用struct.unpack_from函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何类似于struct.unpact_from()的c或c ++函数?
可能吗?
或至少告诉我这三个陈述有什么区别??

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)

推荐答案

^ ]

解释了此功能和其他功能.
它们非常特定于python,C或C ++本身不支持这种功能.

有一个库,但是您可以在这里查看:

http://stackoverflow.com/questions/1550721/python-struct-pack-equivalent- in-c [^ ]
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[^]


python中的struct库旨在模拟低级"语言(如C/C ++)的二进制序列化,而不必模拟python struct C/C ++中的库,如果您使用固定的预定义数据结构!可以将3个字符串-``> LL''及其朋友-转换为它们各自的C结构等效项.如果您的程序必须在不同的体系结构上运行,那么您在C/C ++中所要做的只是处理字节序.
例如,C中的``> LL''是这样的:
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)


由于``> LL''格式字符串中的``>''字符表示数据始终为大端字节序,因此在稍稍进行序列化或反序列化时,必须交换结构中每个整数的字节顺序字节序体系结构. Philip Stuyck链接了一个struct-pack-equivalent-in-c,但实际上很少需要.人们通常从不使用python的结构库对C结构具有的额外功能,即-您不必在编译时就知道结构的格式.在python中,您甚至可以通过网络接收结构的格式-但实际上,我本人从未见过类似的东西.如果您的python程序使用固定数量的预定义结构格式字符串(如> LL"),则应将其转换为等效的C结构,并在必要时处理字节序.确保在您的python格式字符串中,您还使用与C代码中相同的对齐方式-这就是为什么存在pragma包的原因!有关格式字符串的规范,请参见: http://docs.python.org/library/struct.html#struct-对齐 [ ^ ]

Java和python等高级语言没有机制来处理结构化的二进制数据(如C/C ++中的sturct),但是有时您必须处理由某些C/C ++程序序列化的二进制数据-这就是为什么struct库存在于python中.它具有比C/C ++强大的功能(动态行为)的事实很少被利用,并且可以在C/C ++中以其他更友好的性能实现.与C/C ++中的本机结构一起使用,不太可能需要使用C/C ++库,该库通过向程序中引入复杂性和性能降级来模仿python结构的额外不必要的功能!
在您的情况下,结构包含相同的成员数据类型,因此在C/C ++中,您只需声明一个数组即可获得与python格式字符串相同的二进制结构:

''> LL'':


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[^]

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'':


''>256L'':

unsigned long[256];


''> 64L'':


''>64L'':

unsigned long[64];


这篇关于混淆在c或c ++中使用struct.unpack_from函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆