c_cpp 样本二进制表数据交换语言设置。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 样本二进制表数据交换语言设置。相关的知识,希望对你有一定的参考价值。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
/* prototype for binary dynamic table language
*/
/* @brief Used to traverse table data and dynamically type
* binary data.
*/
enum item_type_t {
item_type_none,
item_type_bool
};
/*
* @brief Main object that encapsulates a table.
* @data An array of bytes holding the size, length,
* and object specific items of a table.
*/
struct table_t {
unsigned char* data;
size_t size;
size_t item_count;
};
// Fast inline function that writes sizing info to head of table.
static inline void
_write_size_info(unsigned char* b, size_t size, size_t items) {
*(uint64_t*)(b) = (uint64_t)size;
*(uint64_t*)(b + sizeof(uint64_t)) = (uint64_t)items;
}
static void
_table_init(struct table_t* table, size_t size, size_t items) {
table->data = malloc(size);
table->size = size;
table->item_count = items;
_write_size_info(table->data, size, items);
}
int main(int argc, char const *argv[])
{
struct table_t foo;
_table_init(&foo, 50, 5);
printf("size is %llu, item count is %llu\n", *(uint64_t*)(foo.data), *(uint64_t*)(foo.data + sizeof(uint64_t)));
return 0;
}
以上是关于c_cpp 样本二进制表数据交换语言设置。的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp C中的单链表 - 现在只实现交换
c_cpp C语言二进制文件读写
c_cpp 交换链接列表中的节点而不交换数据
c_cpp dlib图样本
c_cpp 内存泄漏样本
c_cpp 样本文件