msg00983.html

1998Q3/msg00983.html · 8.8 KB

From the archive 1998Q3.zip

Download the archive   « Back to the archive contents

<!-- MHonArc v2.4.4 -->
<!--X-Subject: [MUD&#45;Dev] Re: [CODE QUESTION] How to encode floats into bytes? -->
<!--X-From-R13: Dvpuneq Ibbypbpx <YnHveNqvny.cvcrk.pbz> -->
<!--X-Date: Mon, 7 Sep 1998 11:05:28 &#45;0700 -->
<!--X-Message-Id: 35F491E2.B30#dial,pipex.com -->
<!--X-Content-Type: text/plain -->
<!--X-Reference: Pine.LNX.3.96.980907104250.32349B&#45;100000#shamen,cyberhighway.net -->
<!--X-Head-End-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>MUD-Dev message, [MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?</title>
<!-- meta name="robots" content="noindex,nofollow" -->
<link rev="made" href="mailto:KaVir#dial,pipex.com">
</head>
<body background="/backgrounds/paperback.gif" bgcolor="#ffffff"
      text="#000000" link="#0000FF" alink="#FF0000" vlink="#006000">

  <font size="+4" color="#804040">
    <strong><em>MUD-Dev<br>mailing list archive</em></strong>
  </font>
      
<br>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
<br clear=all><hr>
<!--X-Body-Begin-->
<!--X-User-Header-->
<!--X-User-Header-End-->
<!--X-TopPNI-->

Date:&nbsp;
[&nbsp;<a href="msg00982.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00984.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Thread:&nbsp;
[&nbsp;<a href="msg00982.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00994.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Index:&nbsp;
[&nbsp;<A HREF="author.html#00983">Author</A>
&nbsp;|&nbsp;<A HREF="#00983">Date</A>
&nbsp;|&nbsp;<A HREF="thread.html#00983">Thread</A>
&nbsp;]

<!--X-TopPNI-End-->
<!--X-MsgBody-->
<!--X-Subject-Header-Begin-->
<H1>[MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?</H1>
<HR>
<!--X-Subject-Header-End-->
<!--X-Head-of-Message-->
<UL>
<LI><em>To</em>: <A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A></LI>
<LI><em>Subject</em>: [MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes? </LI>
<LI><em>From</em>: Richard Woolcock &lt;<A HREF="mailto:KaVir#dial,pipex.com">KaVir#dial,pipex.com</A>&gt;</LI>
<LI><em>Date</em>: Mon, 07 Sep 1998 19:09:38 -0700</LI>
<LI><em>Reply-To</em>: <A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A></LI>
</UL>
<!--X-Head-of-Message-End-->
<!--X-Head-Body-Sep-Begin-->
<HR>
<!--X-Head-Body-Sep-End-->
<!--X-Body-of-Message-->
<PRE>
Ben Greear wrote:
&gt; 
&gt; On Mon, 7 Sep 1998, T. Alexander Popiel wrote:
&gt; 
&gt; &gt; In message:  &lt;<A HREF="msg00976.html">Pine.LNX.3.96.980906215952.30075A-100000#shamen,cyberhighway.net</A>&gt;
&gt; &gt;              Ben Greear &lt;greear#cyberhighway,net&gt; writes:
&gt; &gt; &gt;
&gt; &gt; &gt;What is the standard way (if there is one) to encode floating
&gt; &gt; &gt;point numbers (double too I guess) into bytes for transport accross
&gt; &gt; &gt;the network.
&gt; &gt; &gt;
&gt; &gt; &gt;I'm using c++, on both ends (encode/decode).
&gt; &gt; &gt;
&gt; &gt; &gt;It would seem like there would be a method to do this somewhere!
&gt; &gt;
&gt; &gt; Standard is to send raw IEEE format across the wire; just about
&gt; &gt; everyone uses IEEE internally these days, and those who don't
&gt; &gt; know how to convert, so they can talk with those that do.
&gt; &gt;
&gt; &gt; IEEE 32-bit format is bigendian: 1 sign bit, 8 bits exponent in
&gt; &gt; offset 127 notation, implied 1 (not stored), 23 bits mantissa.
&gt; &gt; Special codes are used in the exponent to denote NaN, +/-Inf,
&gt; &gt; and reduced-precision numbers between 2^-128 and 2^-152.
&gt; &gt;
&gt; &gt; IEEE 64-bit is similar, with 16 bits exponent and 47 bits
&gt; &gt; mantissa.
&gt; &gt;
&gt; &gt; If you do the conversion manually, actually check the standard;
&gt; &gt; I prolly have an off-by-one error in the above (which was taken
&gt; &gt; from memory).
&gt; 
&gt; I was looking at the standard last night..  You are at least
&gt; very close :)  (Don't remember all the details myself.)
&gt; 
&gt; I'm going to play around with bit-masking and casting to see if
&gt; I can get at the bits w/out turning the target into an integer!

How about

typedef union
{
   unsigned long Int;
   float         Float;
} int_float_type;

typedef union
{
   unsigned long Int[2];
   double        Double;
} int_double_type;

(copying the example from your other post)

   file_descriptor descriptor;  //assume it's connected appropriately.
   float           f_data = 42.5;
   int_float_type  if_data;
   if_data.Float = f_data;
   write(descriptor, if_data.Int, 0, 4);      //think those args are right..

And for doubles:

   file_descriptor descriptor;  //assume it's connected appropriately.
   double          d_data = 42.5;
   int_double_type id_data;
   id_data.Double = f_data;
   write(descriptor, if_data.Int[0], 0, 4);      //think those args are right..
   write(descriptor, if_data.Int[1], 0, 4);      //think those args are right..

Or maybe those last two lines could be replaced with:

   write(descriptor, if_data.Int, 0, 8);         //think those args are right..

Just a suggestion, don't know if it'll work though.

KaVir.


</PRE>

<!--X-Body-of-Message-End-->
<!--X-MsgBody-End-->
<!--X-Follow-Ups-->
<HR>
<!--X-Follow-Ups-End-->
<!--X-References-->
<UL><LI><STRONG>References</STRONG>:
<UL>
<LI><STRONG><A NAME="00982" HREF="msg00982.html">[MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?</A></STRONG>
<UL><LI><EM>From:</EM> Ben Greear &lt;greear#cyberhighway,net&gt;</LI></UL></LI>
</UL></LI></UL>
<!--X-References-End-->
<!--X-BotPNI-->
<UL>
<LI>Prev by Date:
<STRONG><A HREF="msg00982.html">[MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?</A></STRONG>
</LI>
<LI>Next by Date:
<STRONG><A HREF="msg00984.html">[MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?</A></STRONG>
</LI>
<LI>Prev by thread:
<STRONG><A HREF="msg00982.html">[MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?</A></STRONG>
</LI>
<LI>Next by thread:
<STRONG><A HREF="msg00994.html">[MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?</A></STRONG>
</LI>
<LI>Index(es):
<UL>
<LI><A HREF="index.html#00983"><STRONG>Date</STRONG></A></LI>
<LI><A HREF="thread.html#00983"><STRONG>Thread</STRONG></A></LI>
</UL>
</LI>
</UL>

<!--X-BotPNI-End-->
<!--X-User-Footer-->
<!--X-User-Footer-End-->
<ul><li>Thread context:
<BLOCKQUOTE><UL>
<LI><STRONG>[MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?</STRONG>, <EM>(continued)</EM>
<ul compact>
<ul compact>
<ul compact>
<ul compact>
<ul compact>
<LI><strong><A NAME="00995" HREF="msg00995.html">[MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?</A></strong>, 
Ola Fosheim Grøstad <a href="mailto:olag#ifi,uio.no">olag#ifi,uio.no</a>, Wed 09 Sep 1998, 19:14 GMT
<UL>
<LI><strong><A NAME="00996" HREF="msg00996.html">[MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?</A></strong>, 
Travis S. Casey <a href="mailto:efindel#io,com">efindel#io,com</a>, Wed 09 Sep 1998, 19:43 GMT
<UL>
<LI><strong><A NAME="01004" HREF="msg01004.html">[MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?</A></strong>, 
Ola Fosheim Grøstad <a href="mailto:olag#ifi,uio.no">olag#ifi,uio.no</a>, Thu 10 Sep 1998, 08:07 GMT
</LI>
</UL>
</LI>
</UL>
</LI>
</ul>
</ul>
</ul>
<LI><strong><A NAME="00982" HREF="msg00982.html">[MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?</A></strong>, 
Ben Greear <a href="mailto:greear#cyberhighway,net">greear#cyberhighway,net</a>, Mon 07 Sep 1998, 17:47 GMT
<UL>
<LI><strong><A NAME="00983" HREF="msg00983.html">[MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?</A></strong>, 
Richard Woolcock <a href="mailto:KaVir#dial,pipex.com">KaVir#dial,pipex.com</a>, Mon 07 Sep 1998, 18:05 GMT
</LI>
</UL>
</LI>
</ul>
<LI><strong><A NAME="00994" HREF="msg00994.html">[MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?</A></strong>, 
J C Lawrence <a href="mailto:claw#under,engr.sgi.com">claw#under,engr.sgi.com</a>, Wed 09 Sep 1998, 17:33 GMT
<UL>
<LI><strong><A NAME="00997" HREF="msg00997.html">[MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?</A></strong>, 
Adam J. Thornton <a href="mailto:adam#phoenix,Princeton.EDU">adam#phoenix,Princeton.EDU</a>, Wed 09 Sep 1998, 22:30 GMT
<UL>
<LI><strong><A NAME="01000" HREF="msg01000.html">[MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?</A></strong>, 
Ben Greear <a href="mailto:greear#cyberhighway,net">greear#cyberhighway,net</a>, Thu 10 Sep 1998, 01:31 GMT
</LI>
</UL>
</LI>
</UL>
</LI>
</ul>
</LI>
<LI><strong><A NAME="00975" HREF="msg00975.html">[MUD-Dev] (fwd) CODE RELEASE: [server] SlashMUD (MacOS mud server)</A></strong>, 
J C Lawrence <a href="mailto:claw#greek,kanga.nu">claw#greek,kanga.nu</a>, Sun 06 Sep 1998, 22:04 GMT
</UL></BLOCKQUOTE>

</ul>
<hr>
<center>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
</center>
<hr>
</body>
</html>

Site Stats

Snippets: 1791
Codebases: 571