Skip to content

Instantly share code, notes, and snippets.

@adyatlov
Created April 23, 2013 13:32
Show Gist options
  • Select an option

  • Save adyatlov/5443571 to your computer and use it in GitHub Desktop.

Select an option

Save adyatlov/5443571 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
union FloatByteStorage {
char bytes[4];
float number;
};
int main(int, char**) {
FloatByteStorage storage;
// Encoding
storage.number = 0.543;
printf("%f consists of %X:%X:%X:%X\n", storage.number, storage.bytes[0], storage.bytes[1], storage.bytes[2], storage.bytes[3]);
// Decoding
storage.bytes[0] = 0xC; storage.bytes[1] = 0x2; storage.bytes[2] = 0xB; storage.bytes[3] = 0x3F;
printf("%X:%X:%X:%X compose %f\n", storage.bytes[0], storage.bytes[1], storage.bytes[2], storage.bytes[3], storage.number);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment