Hi I'm working on socket translation between two protocols. I read from a binary file and store the parsed header into an array of type uint32_t. Then I grab the fields out of the array and convert them into respective types. So far uint32_t
/int32_t
/uint16_t
to int32_t
works fine.
However, I get all kinds of wrong outputs when trying to combine two uint32_t (append one after the other) and then converting this 64bit long data into a double.
Being a newbie to C programming, I'm struggling with the computer methodology of double
/ float
representation.
Basically what I want to do is: without altering the bit pattern of the two uint32_t
, concast concatenate one after the other to make a 64-bit data, then convert the data as a double
. The most important thing is not to alter the bit pattern as that part of the bit stream is supposed to be a double
.
The following is part of the code:
uint32_t* buffer = (uint32_t*) malloc (arraySize * sizeof(uint32_t));
...
double outputSampleRate = ((union { double i; double outputSampleRate; })
{ .i = ((uint64_t)buffer[6] << 32 | (uint64_t)buffer[7])}).outputSampleRate;
Data in input file:
35.5
value after my code:
4630192998146113536.000000
Also, is there a better way to handle the socket header parsing?