gpt4 book ai didi

php - 可以在 php 中用位域封装一个 c 结构吗?

转载 作者:行者123 更新时间:2023-11-30 14:52:02 25 4
gpt4 key购买 nike

大家好,我有这个 C 结构,我想知道是否可以将它打包在 php fpr 中写入二进制文件:

C 中的结构是:

struct Date
{
unsigned spare : 6;
unsigned day : 6;
unsigned month : 4;
unsigned year : 16
};

我阅读了 php 复制来实现二进制包的 perl 文档,发现您可以打包结构,但没有带位字段的结构示例

用 PHP 生成有什么想法吗?

最佳答案

我通过向 php 添加自定义扩展解决了这个问题:

PHP_FUNCTION(custom_pack)
{
long ts;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ts ) == FAILURE) {
RETURN_NULL();
}

time_t t = ts;

struct tm tm = *localtime(&t);

struct MariaDBColmunStoreDateTime the_date;
the_date.msecond = 000000;
the_date.second = tm.tm_sec ;
the_date.minute = tm.tm_min;
the_date.hour = tm.tm_hour;
the_date.day = tm.tm_mday;
the_date.month = tm.tm_mon + 1;
the_date.year = tm.tm_year + 1900;


char str[16];

sprintf(str,"%016llx",the_date);

strcpy(str, str_reverse_in_place(str,16));

RETURN_STRING(str, 1);

}

关于php - 可以在 php 中用位域封装一个 c 结构吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47804790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com