/* vim: ft=c ff=unix fenc=utf-8
 * file: xx.c
 */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <math.h>

/* input bin data *MUST* be eq 32 bits in little-endian byte order */
float
bin2float (unsigned char *bin) {
	float result = 1.f; /* declare with imaginary unit */
	int e = bin[3] - 127;
	int bitno;
	/* first byte (7 bits) */
	for (bitno = 7; bitno > -1; bitno--)
		if (bin[2] & (1 << bitno))
			result += pow(2, -1 * (7 - bitno));
	/* second octet */
	for (bitno = 8; bitno > -1; bitno--)
		if (bin[1] & (1 << bitno))
			result += pow(2, -1 * (8 - bitno + 7));
	/* third octet */
	for (bitno = 8; bitno > -1; bitno--)
		if (bin[0] & (1 << bitno))
			result += pow(2, -1 * (8 - bitno + 15));
	result *= pow(2, e);
	/* check invert bit */
	if (bin[2] & 0x80)
		result *= -1;
	return result;
}

int
main (int argc, char *argv[])
{
	unsigned char x[] = {0x00, 0x00, 0x72, 0x81};
	printf("%f\n", bin2float(x));
	return EXIT_SUCCESS;
}

