Created
April 16, 2013 17:22
-
-
Save catatsuy/5397767 to your computer and use it in GitHub Desktop.
from float/double to byte sequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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