Having a Hex represented as a string in c
eg char* text = "0xffff"
I manage to hold the data in a uint32_t
with the following function:
for (unsigned int i = 0; i < line_length && count < WORD_SIZE; i++) {
char c[2]; //represent the digit as string
c[0] = line[i];
c[1] = '\0';
if (isxdigit(c[0])) { //we've found a relevant char.
res_out <<= 4; // shift left by 4 for the next 4 bits.
res_out += (int32_t)strtol(c, NULL, 16); //set the last 4 bits bit to relevant value
//res_out <<= 4; // shift left by 4 for the next 4 bits.
count += 4;
}
}
Now, having the 32 bits, the uint32_t sometimes represented a single-precision floating point number, and I would like to parse it as such
Using float f = (float)num
of course casts the int representation to float (not the needed operation) and I have no other idea's how to tell memory it's actually a floating point number
Just for future references, As @melpomene suggested
uint32_t x = /* some single precision float value dumped into a uint32_t*/;
uint32_t float_placeholder = 0;
memcpy(&float_placeholder, &x, sizeof(uint32_t));
float_placeholder
holds the true floating point number
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.