473,695 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3586
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.ft ab[i]);
}

for(i = 0; i < sizeof(double); ++i)
{
double_bytes.ft ab[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.ft ab[i]);
}

for(i = 0; i < sizeof(double); ++i)
{
double_bytes.ft ab[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.ft ab[] 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.ft ab[] 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.*********@fa stwebnet.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
9514
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 contains the following three floating-point numbers: 1.0 2.0 3.0
22
3709
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 wintel compilers support a 16-byte "long double" (with 33 decimal digits of accuracy) including proper printf() support. I found some compilers that did support "long double", but theirs was an 8-byte or 10-byte or 12-byte type with accuracy the...
3
2608
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, sizeof(double)=8 and sizeof(long double)=10 (if 'long' or 'double' sizeof(Header)=8 if 'long double' sizeof(Header)=12)
9
6756
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 get the correct answer by taking the decimal valueand using that as an index to an array that will map to thecorrect EBCDIC value in hex. By larger values, an example would be "AA" in EBCDIC hex wouldgive me the value of 63 in decimal (ASCII) when...
6
5266
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;
11
3586
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 Read_bin(ByVal ruta As String) Dim cadena As String = "" Dim dato As Array If File.Exists(ruta) = True Then
5
6628
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 an unsigned short int is 16-bits). For example, the double precision representation of 1/3 is 3fd5 5555 5555 5555 (hex).
13
1531
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 into an arraylist? I need to do some sort of commas split to just read the values and not the commas. Dim oFile As File Dim oRead As System.IO.StreamReader Dim EntireFile As String
4
2670
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 Integer aa2 As Integer aa3 As Integer aa4 As Integer
0
8625
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8565
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9113
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8977
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8841
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4339
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2269
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.