Skip to content

Instantly share code, notes, and snippets.

@catatsuy
Created April 16, 2013 17:22
Show Gist options
  • Save catatsuy/5397767 to your computer and use it in GitHub Desktop.
Save catatsuy/5397767 to your computer and use it in GitHub Desktop.
from float/double to byte sequence
#include <cstdio>
void dump(unsigned char *p, int n) {
for (int i = n - 1; i >= 0; i--) {
printf("%02x ", p[i]);
}
printf("\n");
}
int main() {
int i;
union {
double d;
float f;
unsigned char c[8];
} uni;
printf("数値:");
scanf("%lf", &uni.d);
printf("d = %lf\n", uni.d);
dump(uni.c, 8);
uni.f = uni.d;
printf("f = %f\n", uni.f);
dump(uni.c, 4);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment