首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从C中的填充位结构求出划时代的时间值

从C中的填充位结构求出划时代的时间值
EN

Stack Overflow用户
提问于 2012-03-29 14:13:01
回答 1查看 236关注 0票数 1

我需要从具有以下数据结构(它是12字节结构)的二进制数据文件中找出Epoch时间:

代码语言:javascript
复制
Field-1 : Byte 1, Byte 2, + 6 Bits from Byte 3
Time-1  :                   2 Bits from Byte 3 + Byte 4
Time-2  : Byte 5, Byte 6, Byte 7, Byte 8
Field-2 : Byte 9, Byte 10, Byte 11, Byte 12

对于字段-1和字段-2,我没有问题,因为它们可以很容易地取出。

我需要在Epoch time (long)中的时间值,因为它以Bytes 5、6、7、8、3和4包装如下:

  • Bytes 5到8 (32位字)从0到31 (字节5有0到7位,字节6有8到15,字节7有16到23,字节8有24到31)。

其余10位时间值以Bytes 3和字节4包装如下:

  • 字节3有2位:32和33,字节4有剩余位: 34到41。

因此,时间值的总位数是42位,如上面所示。

我需要计算出这42位的历元值。我该怎么做呢?

我做过这样的事情,但不确定它是否给了我正确的价值:

代码语言:javascript
复制
typedef struct P_HEADER {
    unsigned int tmuNumber : 22; //sorry for the typo.
    unsigned int time1 : 10; // Bits 6,7 from Byte-3 + 8 bits from Byte-4
    unsigned int time2 : 32; // 32 bits: Bytes 5,6,7,8
    unsigned int traceKey : 32; 
} __attribute__((__packed__)) P_HEADER;

然后在代码中:

代码语言:javascript
复制
P_HEADER *header1;

//get input string in hexa,etc..etc..
//parse the input with the header as :
header1 = (P_HEADER *)inputBuf;
// then print the header1->time1, header1->time2 ....
long ttime = header1->time1|header1->time2;

这就是释放价值观的方法吗?

EN

回答 1

Stack Overflow用户

发布于 2012-03-29 14:41:08

这将为您提供所需的价值:

代码语言:javascript
复制
typedef struct P_HEADER {
    unsigned int tmuNumber : 22;
    unsigned int time1 : 10; // Bits 6,7 from Byte-3 + 8 bits from Byte-4
    unsigned int time2 : 32; // 32 bits: Bytes 5,6,7,8
    unsigned int traceKey : 32; 
} __attribute__((__packed__)) P_HEADER;

long ttime = ((uint64_t)header1->time1) << 32 | header1->time2;

只是在小终端机器上才有这样的效果。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9927375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档