这个python代码的C等价物是什么? 谢谢。在
data = gather_me_some_data()
# where data = [ (metic, datapoints), ... ]
# and datapoints = [ (timestamp, value), ... ]
serialized_data = cPickle.dumps(data, protocol=-1)
length_prefix = struct.pack("!L", len(serialized_data))
message = length_prefix + serialized_data
如果您可以使用C++,则存在{a1}
C不支持直接序列化机制,因为在C中您无法在运行时获取类型信息。您必须自己在运行时注入一些类型信息,然后根据该类型信息构造所需的对象。因此,请定义所有可能的结构:
然后定义enum,它收集关于您总共拥有哪些结构的信息:
^{pr2}$定义帮助器函数,该函数用于确定任何结构大小:
然后定义序列化函数:
和反序列化函数:
序列化用法示例:
我认为这是C语言中最简单的序列化方法,但它有一些问题:
char *
转换为整型,如enum
](…或重构代码以使其更可移植)。在相关问题 更多 >
编程相关推荐