I want to convert Double Values (including the decimal place) to Character and store them as bytes.
I follow the simple instructions from
http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/d606a107-d093-4f00-b585-cdf97f09fa76
but it does not seem to work. Where did I go wrong? thanks.
my simple code:
////////////////////////////////////////
#include <stdio.h>
int main()
{
int i;
double a = 123.45678;
char buffer[sizeof(a)];
*(double*)buffer = a;
for (i=0;i<sizeof(a);i++)
{
printf("buffer = %X\n", buffer[i]);
}
return 0;
}
///// output
buffer = FFFFFFE1
buffer = 5D
buffer = 2E
buffer = FFFFFFE2
buffer = 3B
buffer = FFFFFFDD
buffer = 5E
buffer = 40

3 answers
To get rid of the leading FFFFFF's, you need to define the buffer as unsigned:
The code, of course, gives you the underlying byte representation of a double which is always 8 bytes long whether there's a decimal point or not.
Incidentally, you don't need to drop down to C to do this sort of thing. The equivalent in C# would be:
answered one year ago by:
17279
494
hi Vuples, can I have your equivalent code in C, instead of C#? The reason is that I am doing this for microcontroller, need to be in C. thanks.
17279
It's just as you already had it but with the buffer declared as 'unsigned'.
I actually try both of this, and they produce the same output.
///////////////
#include <stdio.h>
int main()
{
int i;
double a = 123.45678;
//typedef unsigned char byte;
unsigned char buffer[sizeof(a)];
*(double*)buffer = a;
//byte* pb = (byte*)&a;
for (i=0;i<sizeof(a);i++)
{
printf("buffer = %X\n", buffer[i]);
}
return 0;
}
////////
#include <stdio.h>
int main()
{
int i;
double a = 123.45678;
typedef unsigned char byte;
unsigned char buffer[sizeof(a)];
//*(double*)buffer = a;
byte* pb = (byte*)&a;
for (i=0;i<sizeof(a);i++)
{
// printf("buffer = %X\n", buffer[i]);
printf("buffer = %X\n", *pb++);
}
return 0;
}
////////
output
buffer = E1
buffer = 5D
buffer = 2E
buffer = E2
buffer = 3B
buffer = DD
buffer = 5E
buffer = 40
But I do not understand how about output is derived from double a = 123.45678
Could you elaborate?
I know HEX representation is using base of 16, but how do the decimal dot being represented? if the double is a negative value, it would have different consideration too.
http://www.learn-c.com/data_lines.htm
Say if my a = 1.2994745, representing them in 8-bytes character, would there be some truncation error? For 8-bytes character representation, what is the maximum number of decimal places to be no truncation error?
answered one year ago by:
494
Doubles in both C and C# are stored in what's known as IEEE 754-1985 format (also known as IEC 60559).
The details of this format are complex (see here for full details) but what it boils down to is that any real number with an absolute value between (approx) 5.0 * 10 ^ -324 and 1.7 * 10 ^ 308 can be stored with up to 15 or 16 digit precision as well as the special values: Infinity, Minus Infinity, Minus Zero and NaN ('not a number'). The special values ensure that double arittmetic never overflows or underflows.
In practice, double arithmetic is done internally to 10 bytes of precision and the result rounded off to 8 bytes.
In C you actually have a 'long double' type (10 bytes, 19 digits of precision) but this isn't supported in C#.
answered one year ago by:
17279