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 15 3555
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
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.
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
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
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
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.
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
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;)
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
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
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
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.
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Yeow |
last post by:
hello,
i was trying to use the fread function on SunOS and ran into some
trouble.
i made a simple test as follows:
i'm trying to read in a binary file (generated from a fortran code)
that...
|
by: bq |
last post by:
Hello,
Two questions related to floating point support:
What C compilers for the wintel (MS Windows + x86) platform are C99
compliant as far as <math.h> and <tgmath.h> are concerned?
What...
|
by: RoSsIaCrIiLoIA |
last post by:
I have rewrote the malloc() function of K&R2 chapter 8.7
typedef long Align;
^^^^
Here, should I write 'long', 'double' or 'long double'?
I know that in my pc+compiler sizeof(long)=4,...
|
by: jeff M via .NET 247 |
last post by:
I'm still having problems reading EBCDIC files. Currently itlooks like the lower range (0 to 127) is working. I have triedthe following code pages 20284, 20924, 1140, 37, 500 and 20127.By working I...
|
by: arne.muller |
last post by:
Hello,
I've come across some problems reading strucutres from binary files.
Basically I've some strutures
typedef struct {
int i;
double x;
int n;
double *mz;
|
by: Freddy Coal |
last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all
the file in a String.
I make this function for make that:
'--------------------------
Public Shared Function...
|
by: anders.weitman |
last post by:
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...
|
by: cmdolcet69 |
last post by:
I have this code below that will open a created file and read the file
to the end. However if my file has values 200,300,400 seperated by
commas how can i read in each seperate value and place it...
|
by: knuckels23 |
last post by:
Hi All,
I have a Random access file which is written using VB 6.0.
I need to read this file using C#.
The record used in VB to write the Random access file is as follows
Type AA
aa1 As...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |