reading a double var byte per byte

J

jeko

Hi,
is it possible to read a double variable one byte at a time.
I would want store each byte into unsigned char and then assemble again
the double variable again.

thanx

Andrea
 
P

Pierre Maurette

jeko a écrit :
Hi,
is it possible to read a double variable one byte at a time.
I would want store each byte into unsigned char and then assemble again
the double variable again.
Maybe:
int i;
union {
double f;
unsigned char ftab[sizeof(double)];
}double_bytes;

double_bytes.f = 123.54;

for(i = 0; i < sizeof(double); ++i)
{
printf("%d\t", double_bytes.ftab);
}

for(i = 0; i < sizeof(double); ++i)
{
double_bytes.ftab = 0;
}

printf("\n%f\n", double_bytes.f);
 
J

jeko

Pierre Maurette ha scritto:
jeko a écrit :
Hi,
is it possible to read a double variable one byte at a time.
I would want store each byte into unsigned char and then assemble
again the double variable again.

Maybe:
int i;
union {
double f;
unsigned char ftab[sizeof(double)];
}double_bytes;

double_bytes.f = 123.54;

for(i = 0; i < sizeof(double); ++i)
{
printf("%d\t", double_bytes.ftab);
}

for(i = 0; i < sizeof(double); ++i)
{
double_bytes.ftab = 0;
}

printf("\n%f\n", double_bytes.f);


Ok,
but how copy double into unsigned char vector?
In your code initialize only the vector with 0 value.
 
P

Pierre Maurette

jeko a écrit :
[...]
Ok,
but how copy double into unsigned char vector?
In your code initialize only the vector with 0 value.
Maybe the problem is my english ;-)
(my italian is worse).
But:

double any_float = 123.456;
union {
double f;
unsigned char ftab[sizeof(double)];
}double_bytes;
double_bytes.f = any_float;

initialize the vector double_bytes.ftab[] with any_float bytes, no ?
 
J

jeko

Pierre Maurette ha scritto:
jeko a écrit :
[...]
Ok,
but how copy double into unsigned char vector?
In your code initialize only the vector with 0 value.

Maybe the problem is my english ;-)
(my italian is worse).
But:

double any_float = 123.456;
union {
double f;
unsigned char ftab[sizeof(double)];
}double_bytes;
double_bytes.f = any_float;

initialize the vector double_bytes.ftab[] with any_float bytes, no ?
You r great!!!I'm italian really;)
Your trick is correct!
I had forgotten union statement :)
thanx
 
P

Pierre Maurette

jeko a écrit :
[...]
About union: is correct touse unsigned char vector when union is
intialized with a double? On the Kernighan Manual this is an incorrect use.
I don't know. Maybe not 100% standard compliant.
But is "store each byte [of a double variable] into unsigned char" a
standard compliant question ?
 
J

jeko

Pierre Maurette ha scritto:
jeko a écrit :
[...]
About union: is correct touse unsigned char vector when union is
intialized with a double? On the Kernighan Manual this is an incorrect
use.

I don't know. Maybe not 100% standard compliant.
But is "store each byte [of a double variable] into unsigned char" a
standard compliant question ?

It's true....you are a philosopher;)
 
R

Richard Bos

jeko said:
Pierre Maurette ha scritto:

Definitely, however...
int i;
union {
double f;
unsigned char ftab[sizeof(double)];
}double_bytes;

....this is not the solution. Once you've assigned a value to one union
member, the values of all others become undefined (or was it
unspecified? Unreliable, in any case).
but how copy double into unsigned char vector?

There's no such thing as "vectors" in C (unless you create a
user-defined type or alias with that name, of course). What Pierre uses
is an array.

The proper solution is either this:

double d=123.456;
unsigned char *ptr;

for (ptr=(unsigned char *)&d; ptr<=(unsigned char *)(d+1); ptr++) {
/* Do something with the next byte, i.e. *ptr, for example: */
printf("%u\n", *ptr); /* Note no cast: integer conversions again. */
}

or, if you want to store the double's representation for later:

double d=123.456;
unsigned char arr[sizeof double];

memcpy(arr, &d, sizeof d);

Richard
 
P

pete

jeko said:
Hi,
is it possible to read a double variable one byte at a time.
I would want store each byte into unsigned char
and then assemble again
the double variable again.

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
const double source = 3.14159265;
double destination;
size_t byte;
unsigned char array[sizeof destination];

byte = sizeof array;
while (byte-- != 0) {
array[byte] = ((unsigned char *)&source)[byte];
}
byte = sizeof array;
while (byte-- != 0) {
((unsigned char *)&destination)[byte] = array[byte];
}
printf("source is %f\n", source);
printf("destination is %f\n", destination);
return 0;
}

/* END new.c */
 
R

Richard Bos

Pierre Maurette said:
jeko a écrit :
[...]
About union: is correct touse unsigned char vector when union is
intialized with a double? On the Kernighan Manual this is an incorrect use.
I don't know. Maybe not 100% standard compliant.

100% not Standard compliant, rather.
But is "store each byte [of a double variable] into unsigned char" a
standard compliant question ?

Yes.

Richard
 
I

infobahn

Pierre said:
jeko a écrit :
[...]
About union: is correct touse unsigned char vector when union is
intialized with a double? On the Kernighan Manual this is an incorrect use.
I don't know. Maybe not 100% standard compliant.

#include <stdio.h>

int main(void)
{
union
{
double d;
unsigned char uca[sizeof(double)];
} u;
size_t i;
u.d = 3.1415926;

printf("The object representation of %f is ", u.d);

for(i = 0; i < sizeof u.uca; i++)
{
printf("%02X", u.uca);
}
printf("\n\nSee 6.2.6.1(4) of C99.\n");

return 0;
}
But is "store each byte [of a double variable] into unsigned char" a
standard compliant question ?

If you mean "does it make sense within the context of the C language
to ask about getting at the bytes of a double via some technique
involving unsigned chars?", then the answer is yes, it does.
 
P

pete

Richard said:
jeko said:
Pierre Maurette ha scritto:

Definitely, however...
int i;
union {
double f;
unsigned char ftab[sizeof(double)];
}double_bytes;

...this is not the solution. Once you've assigned a value to one union
member, the values of all others become undefined (or was it
unspecified? Unreliable, in any case).
but how copy double into unsigned char vector?

There's no such thing as "vectors" in C (unless you create a
user-defined type or alias with that name, of course). What Pierre uses
is an array.

The proper solution is either this:

double d=123.456;
unsigned char *ptr;

for (ptr=(unsigned char *)&d; ptr<=(unsigned char *)(d+1); ptr++) {
/* Do something with the next byte, i.e. *ptr, for example: */
printf("%u\n", *ptr); /* Note no cast: integer conversions again. */
}

It should be
ptr != (unsigned char *)(&d+1)
instead of
ptr<=(unsigned char *)(d+1)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top