A
anders.weitman
Hi!
I want to get the representation in memory of a variable of type
double and put it in an array of four unsigned short int (I'm on a 32-
bits Windows architecture so a double is 64-bits and an unsigned short
int is 16-bits).
For example, the double precision representation of 1/3 is 3fd5 5555
5555 5555 (hex).
I tried to do a simple cast from a pointer of type double to a pointer
to an array of unsigned short int but does not seem to work. I'm not
very good a C programming so help is very much appreciated.
#include <stdio.h>
void double_to_uint16(double *in, unsigned short int *out)
{
out = (unsigned short int *)in;
}
main()
{
double a = 0.3333333333333333;
unsigned short int b[4];
double_to_uint16(&a, b);
printf("1: %hx\n", b[0]);
printf("2: %hx\n", b[1]);
printf("3: %hx\n", b[2]);
printf("4: %hx\n", b[3]);
}
The program above gives the following printout:
1: f000
2: 6113
3: 0
4: 0
But I expected to get:
1: 3fd5
2: 5555
3: 5555
4: 5555
I want to get the representation in memory of a variable of type
double and put it in an array of four unsigned short int (I'm on a 32-
bits Windows architecture so a double is 64-bits and an unsigned short
int is 16-bits).
For example, the double precision representation of 1/3 is 3fd5 5555
5555 5555 (hex).
I tried to do a simple cast from a pointer of type double to a pointer
to an array of unsigned short int but does not seem to work. I'm not
very good a C programming so help is very much appreciated.
#include <stdio.h>
void double_to_uint16(double *in, unsigned short int *out)
{
out = (unsigned short int *)in;
}
main()
{
double a = 0.3333333333333333;
unsigned short int b[4];
double_to_uint16(&a, b);
printf("1: %hx\n", b[0]);
printf("2: %hx\n", b[1]);
printf("3: %hx\n", b[2]);
printf("4: %hx\n", b[3]);
}
The program above gives the following printout:
1: f000
2: 6113
3: 0
4: 0
But I expected to get:
1: 3fd5
2: 5555
3: 5555
4: 5555