可以在php中打包带有位字段的c结构吗?

问题描述 投票:0回答:1

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

C中的结构是:

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

我读了perl文件,php复制实现二进制包,看看你可以打包结构但没有结构的示例和位域

在PHP中生成的任何想法?

php c struct binary bit
1个回答
0
投票

我通过向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);

}

© www.soinside.com 2019 - 2024. All rights reserved.