开发者

How do I separately convert a struct timeval into two 32 bit variables?

开发者 https://www.devze.com 2023-01-19 22:59 出处:网络
A struct timeval is 64 bit long. I need, for a project, to convert this long (struct timeval) into two 32 bit chunks, and put each chunk into a 开发者_JAVA技巧different variable. How do I do this?

A struct timeval is 64 bit long. I need, for a project, to convert this long (struct timeval) into two 32 bit chunks, and put each chunk into a 开发者_JAVA技巧different variable. How do I do this? Thanx in advance.


uint32_t* values = &timevalstruct;

// depends on endianess

uint32_t v1 = values[0];
uint32_t v2 = values[1];


As an addition to leppie's answer:

union tvs
{
    struct timeval tv;
    struct ints {
        uint32_t v1;
        uint32_t v2;
    };
};

tvs t;
t.tv = timevalstruct;
uint32_t v1 = tv.ints.v1;
uint32_t v2 = tv.ints.v2;

if you dont want to deal with pointers.


See this : http://linux.die.net/man/2/gettimeofday

Can you use tv_sec and tv_usec fields of the timeval structure?


struct timeval tv;
...
uint32_t seconds = tv.tv_sec;
uint32_t micros = tv.tv_usec;

There you go, separated into 32-bit integers.

0

精彩评论

暂无评论...
验证码 换一张
取 消