469,607 Members | 1,925 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,607 developers. It's quick & easy.

reading a double var byte per byte

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
Nov 14 '05 #1
15 3259
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[i]);
}

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

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

--
Pierre
Nov 14 '05 #2
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[i]);
}

for(i = 0; i < sizeof(double); ++i)
{
double_bytes.ftab[i] = 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.
Nov 14 '05 #3
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 ?

--
Pierre
Nov 14 '05 #4
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
Nov 14 '05 #5
jeko a écrit :
Pierre Maurette ha scritto:

[...]
jeko a écrit :

You r great!!!I'm italian really;)

"ha scritto:" !!
My name is Holmes. Sherlock Holmes ;-)
--
Pierre
Nov 14 '05 #6
Pierre Maurette ha scritto:
jeko a écrit :
Pierre Maurette ha scritto:


[...]
jeko a écrit :


You r great!!!I'm italian really;)


"ha scritto:" !!
My name is Holmes. Sherlock Holmes ;-)

eheh.
About union: is correct touse unsigned char vector when union is
intialized with a double? On the Kernighan Manual this is an incorrect use.
Nov 14 '05 #7
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 ?
--
Pierre
Nov 14 '05 #8
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;)
Nov 14 '05 #9
jeko <a.*********@fastwebnet.it> wrote:
Pierre Maurette ha scritto:
jeko a écrit :
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:
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
Nov 14 '05 #10
jeko wrote:

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 */

--
pete
Nov 14 '05 #11
Pierre Maurette <ma************@wanadoo.fr> wrote:
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
Nov 14 '05 #12
Pierre Maurette wrote:

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[i]);
}
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.
Nov 14 '05 #13
On Tue, 18 Jan 2005 16:01:14 +0100, Pierre Maurette wrote:
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;


Using unions for this sort of thing is always iffy, it is the wrong tool
for the job. You can do this more simply and with no questionmarks over
correctness using pointers, e.g.

int main(void)
{
double d = 0.1;
unsigned char *p = (unsigned char *)&d;
int i;

for (i = 0; i < sizeof d; i++)
printf(" %02x", (unsigned)p[i]);

putchar('\n');

return 0;
}
double_bytes.f = 123.54;

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


This is dangerous, C doesn't guarantee that all-bits-zero is a valid
representation for a double. Also while C's type aliasing rules allow
accessing any object as an array of character type (where unsigned char is
the useful one), it doesn't guarantee that an array of character type can
be accessed as a double. Doing this in a union makes the situation more
complex, but not clearly correct.

Lawrence


Nov 14 '05 #14
Richard Bos wrote:

jeko <a.*********@fastwebnet.it> wrote:
Pierre Maurette ha scritto:
jeko a écrit :

> 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:
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)

--
pete
Nov 14 '05 #15
Pierre Maurette <ma************@wanadoo.fr> writes:
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 ?


That should work. You can also use memcpy():

#include <stdio.h>
#include <string.h>

int main(void)
{
double d0 = 123.456;
unsigned char buf[sizeof(double)];
double d1;

memcpy(buf, &d0, sizeof(double));
memcpy(&d1, buf, sizeof(double));

printf("d0 = %f\nd1 = %f\n", d0, d1);

return 0;
}

Output:

d0 = 123.456000
d1 = 123.456000

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Yeow | last post: by
3 posts views Thread by RoSsIaCrIiLoIA | last post: by
9 posts views Thread by jeff M via .NET 247 | last post: by
6 posts views Thread by arne.muller | last post: by
11 posts views Thread by Freddy Coal | last post: by
5 posts views Thread by anders.weitman | last post: by
13 posts views Thread by cmdolcet69 | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.