pete said:
It's a float representation for 63.0
Or more precisely, it's an IEEE 60559 single precision representation
for 63.0. The code below produces the output "63", and is based on the
description here:
http://www.psc.edu/general/software/packages/ieee/ieee.html
The code is not optimised or well tested.
For systems that actually use IEEE single floats, bear in mind that
they might have a different byte order.
Phil T
#include <stdio.h>
#include <math.h>
#include <float.h>
#include <assert.h>
double ieee_single(const void *v)
{
const unsigned char *data = v;
int s, e;
unsigned long src;
long f;
double value;
src = ((unsigned long)data[0] << 24) +
((unsigned long)data[1] << 16) +
((unsigned long)data[2] << 8) +
((unsigned long)data[3]);
s = (src & 0x80000000UL) >> 31;
e = (src & 0x7F800000UL) >> 23;
f = (src & 0x007FFFFFUL);
if (e == 255 && f != 0) {
/* NaN - Not a number */
value = DBL_MAX;
}
else if (e == 255 && f == 0 && s == 1) {
/* Negative infinity */
value = -DBL_MAX;
}
else if (e == 255 && f == 0 && s == 0) {
/* Positive infinity */
value = DBL_MAX;
}
else if (e > 0 && e < 255) {
/* Normal number */
f += 0x00800000UL;
if (s) f = -f;
value = ldexp(f, e - 127 - 23);
}
else if (e == 0 && f != 0) {
/* Denormal number */
if (s) f = -f;
value = ldexp(f, -126 - 23);
}
else if (e == 0 && f == 0 && s == 1) {
/* Negative zero */
value = 0;
}
else if (e == 0 && f == 0 && s == 0) {
/* Positive zero */
value = 0;
}
else {
/* Never happens */
printf("s = %d, e = %d, f = %lu\n", s, e, f);
assert(!"Oops, unhandled case in ieee_single()");
}
return value;
}
int main(void)
{
float f;
unsigned char combo[] = {0x42, 0x7c, 0x00, 0x00};
printf("%g\n", ieee_single(combo));
return 0;
}