473,569 Members | 2,590 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

combining bytes to make a double?

Hi all, I have a question I hope someone can help me with:

I'm doing some communication over a socket, and have need to send
and receive values of type double. I'm doing everything in terms
of single byte reads and writes to the socket, so what I need to do
is take the individual bytes I receive, that represent a double, and
actually turn them into a double. This I'm struggling to figure
out.

I can easily combine the bytes into a container of some sort
(like say a long) that has the same bit pattern as the double
I want. For example, I can get to the point of having a long
that looks like 0001 1110 1000 0000 for 7.5d. But I can't
seem to get that into a double. A simple cast doesn't appear
to work, and I'm not sure what else to try.

Any help or ideas is greatly appreciated.
Thanks,

Phillip Rhodes
--
North Carolina - First In Freedom

Free America - Vote Libertarian
www.lp.org
Jul 23 '05 #1
15 6720
Phillip Rhodes wrote:
Hi all, I have a question I hope someone can help me with:

I'm doing some communication over a socket, and have need to send
and receive values of type double. I'm doing everything in terms
of single byte reads and writes to the socket, so what I need to do
is take the individual bytes I receive, that represent a double, and
actually turn them into a double. This I'm struggling to figure
out.

I can easily combine the bytes into a container of some sort
(like say a long) that has the same bit pattern as the double
I want. For example, I can get to the point of having a long
that looks like 0001 1110 1000 0000 for 7.5d. But I can't
seem to get that into a double. A simple cast doesn't appear
to work, and I'm not sure what else to try.

Any help or ideas is greatly appreciated.
Thanks,

Phillip Rhodes


IF the sender and receiver are the same Operating System
and architecture (e.g. 32 bit Intel on both ends), you could
use a union.
Here are some code snips:

// overlay a double and a char buffer into the same memory
// area.
union DblBuf
{
double d;
char buf[sizeof(double)];
};

// on the sending end...
DblBuf db;

db.d = 123.456;

// sender writes the bytes of the double to the socket
for (int i = 0; i < sizeof(double); i++)
// write db.buf[i] to the socket
// on the receiving end...
DblBuf db;

// receiver reads the bytes of a double from the socket:
for (int i = 0; i < sizeof(double); i++)
// read from the socket into db.buf[i]

// db.d now contains the double.

The above approach won't work if the machines have
different OS or arch (e.g. Intel to Sparc, Win32 to Linux64, etc).
In that case, you need to send ASCII text (e.g. "123.456" across
the socket) and convert it back to a double on the receiving
end.

Larry
Jul 23 '05 #2
Actually, you just have to know the format of the double in the
message. If the sender and receiver just differ in endianness, you
might still find it easiest to decide on say little endian for the
message, and convert the doubles at the sender or receiver as required.

Of course, if performance isn't an issue and you can choose your
message format and the sender and receiver might not have the same
format for doubles, text might be easiest to code and debug. Otherwise
you'll want to use a union as Larry described.

Stuart

Jul 23 '05 #3
Larry I Smith wrote:
// overlay a double and a char buffer into the same memory
// area.
union DblBuf
{
double d;
char buf[sizeof(double)];
};
thanks Larry, the union approach works great. Sadly,
I have used unions so infrequently that I don't think
the idea of doing it that way would have
ever crossed my mind.

The above approach won't work if the machines have
different OS or arch (e.g. Intel to Sparc, Win32 to Linux64, etc).
In that case, you need to send ASCII text (e.g. "123.456" across
the socket) and convert it back to a double on the receiving


right, those issues are why I'm doing all this. I'm defining
the canonical data representation( s) for going over the wire and
the creating the code to normalize everything to that format (as needed)
on the respective platforms. Needless to say, this is gobs of fun. :-)
Thanks again.
TTYL,

Phil
--
North Carolina - First In Freedom

Free America - Vote Libertarian
www.lp.org
Jul 23 '05 #4
On Sun, 10 Jul 2005 00:47:05 +0400, Larry I Smith
<la***********@ verizon.net> wrote:

[]
IF the sender and receiver are the same Operating System
and architecture (e.g. 32 bit Intel on both ends), you could
use a union.


I don't see why one needs a union for that case. You can read from or
write to a double directly.

double d;
write(fd, &d, sizeof(d));
read(fd, &d, sizeof(d));

--
Maxim Yegorushkin
<fi************ ****@gmail.com>
Jul 23 '05 #5
Maxim Yegorushkin wrote:
On Sun, 10 Jul 2005 00:47:05 +0400, Larry I Smith
<la***********@ verizon.net> wrote:

[]
IF the sender and receiver are the same Operating System
and architecture (e.g. 32 bit Intel on both ends), you could
use a union.


I don't see why one needs a union for that case. You can read from or
write to a double directly.

double d;
write(fd, &d, sizeof(d));
read(fd, &d, sizeof(d));


Because the he said in the original post that
single-byte read/write was part of the requirement
(i.e. he can not read/write more than one byte at a time).

Larry
Jul 23 '05 #6
I'm just curious, why do you have to read/write the socket 1 byte at a time?

Polaris

"Phillip Rhodes" <mi*******@nosp am.cpphacker.co .uk> wrote in message
news:bi******** *************@t wister.southeas t.rr.com...
Hi all, I have a question I hope someone can help me with:

I'm doing some communication over a socket, and have need to send
and receive values of type double. I'm doing everything in terms
of single byte reads and writes to the socket, so what I need to do
is take the individual bytes I receive, that represent a double, and
actually turn them into a double. This I'm struggling to figure
out.

I can easily combine the bytes into a container of some sort
(like say a long) that has the same bit pattern as the double
I want. For example, I can get to the point of having a long
that looks like 0001 1110 1000 0000 for 7.5d. But I can't
seem to get that into a double. A simple cast doesn't appear
to work, and I'm not sure what else to try.

Any help or ideas is greatly appreciated.
Thanks,

Phillip Rhodes
--
North Carolina - First In Freedom

Free America - Vote Libertarian
www.lp.org

Jul 23 '05 #7
Polaris wrote:
I'm just curious, why do you have to read/write the socket 1 byte at a time?


It turns out that I don't really. I just got started thinking in terms
of doing it that way for some reason, and that assumption got lodged in
my head. Now that I've thought about it, I'm pretty sure that I can
actually do what I'm trying to do, without having to read each byte in a
separate recv() call.
After all, if I know how many bytes to read, using recv() in a for
loop, it's just the same to use that value in the recv() call itself.

Chalk it up as a "brain fart" I guess. :-)
TTYL,

Phil
--
North Carolina - First In Freedom

Free America - Vote Libertarian
www.lp.org
Jul 23 '05 #8
That's sounds better.

recv() returns the actual number of bytes received. There is a message group
"alt.winsock.pr ogramming", guess that's better place to get more info on
socket programming.

Polaris
"Phillip Rhodes" <mi*******@nosp am.cpphacker.co .uk> wrote in message
news:Tl******** *************@t wister.southeas t.rr.com...
Polaris wrote:
I'm just curious, why do you have to read/write the socket 1 byte at a
time?


It turns out that I don't really. I just got started thinking in terms
of doing it that way for some reason, and that assumption got lodged in
my head. Now that I've thought about it, I'm pretty sure that I can
actually do what I'm trying to do, without having to read each byte in a
separate recv() call.
After all, if I know how many bytes to read, using recv() in a for
loop, it's just the same to use that value in the recv() call itself.

Chalk it up as a "brain fart" I guess. :-)
TTYL,

Phil
--
North Carolina - First In Freedom

Free America - Vote Libertarian
www.lp.org

Jul 23 '05 #9


Phillip Rhodes wrote:
Larry I Smith wrote:
> // overlay a double and a char buffer into the same memory
// area.
union DblBuf
{
double d;
char buf[sizeof(double)];
};


thanks Larry, the union approach works great. Sadly,
I have used unions so infrequently that I don't think
the idea of doing it that way would have
ever crossed my mind.

The above approach won't work if the machines have
different OS or arch (e.g. Intel to Sparc, Win32 to Linux64, etc).
In that case, you need to send ASCII text (e.g. "123.456" across
the socket) and convert it back to a double on the receiving


right, those issues are why I'm doing all this. I'm defining
the canonical data representation( s) for going over the wire and
the creating the code to normalize everything to that format (as needed)
on the respective platforms. Needless to say, this is gobs of fun. :-)


It's also reinventing the wheel. For sending data types and data
structures over a network, by the far the easiest and most
interoperable approach would be to use ASN.1 notation and a BER library
(Basic Encoding Rules) to encode and decode the data on both ends of
the connection. Not only do well-debugged, freely available
implementations exist on every major platform, but the BER notation
being self-describing is quite robust. It is the data encoding format
used by dozens of standards and innumerable RFCs.

The problem with the union approach is that it is very compiler- and
platform-specific which negates much of the usefullness of a network
and its ability to connect machines of different types. But sending
whatever the compiler produces adheres to explicit standard or external
reference and is inherently fragile even between machines of the same
type.

Greg

Jul 23 '05 #10

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

Similar topics

19
2090
by: James Harris | last post by:
My K&R 2nd ed has in the Reference Manual appendix, A7.4.8 sizeof yields the number of BYTES required to store an object of the type of its operand. What happens if C is running on a machine that addresses larger words only? Shouldn't sizeof be defined to return the smallest number of 'storage units' required to store an object of the type of...
25
7850
by: Allan Rydberg | last post by:
hi i'm trying to shift a double, but i'm getting the error message '>>' illegal, left operand double. althought that the manpages say, '>>' and '<<' can be applied for int's only, i was able to use the shift operator on unsigned longs without any problems. does anyone know how to do this properly?
14
6391
by: abhi147 | last post by:
Hi , I want to convert an array of bytes like : {79,104,-37,-66,24,123,30,-26,-99,-8,80,-38,19,14,-127,-3} into Unicode character with ISO-8859-1 standard. Can anyone help me .. how should I go about doing it ? Thanks
3
270
by: Nutkin | last post by:
Hi i have to program a code to perform 1 of 5 functions at the users request, I have got to the part where i have to program the equations and i cant seem to get them to link together. Basicly when the used enters his radians i was to link to a sin x function to calculate the sin of the number also there will be a factorial function and a cos...
64
8310
by: Robert Seacord | last post by:
The C standard doesn't say anything about what happens when you call realloc with a size argument of 0. Both glibc and openbsd appear to return a valid pointer to a zero-sized object.. e.g. the return of a malloc(0). Does anyone know of a runtime where realloc() free'ed the object and then returned NULL? If so, it would make the following...
11
3574
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
5600
by: Tristan Miller | last post by:
Greetings. Is it possible using HTML and CSS to represent a combining diacritical mark in a different style from the letter it modifies? For example, say I want to render Å‘ (Latin small letter o with a double acute accent), but with the o in black and the double acute accent in green. Are either of the following valid? 1. <span...
10
10580
by: H | last post by:
Hi, I have the following address fields in a table: flat_number house_name_or_number street village postal_town county postcode
5
4350
by: girays | last post by:
I want to make a good design about combining information. Suppose you have different data types, and each may have semantically same data. I want to merge these different data types into a single data at run- time. For example, sonar sensor has two internal data (bearing and bearing_rate), and Radar has two as well (range, bearing. struct...
0
7701
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...
0
7615
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...
0
7924
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. ...
1
7677
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...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
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...
0
3653
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...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.