<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Network to / from host conversion</title>
		<link>http://www.allegro.cc/forums/view/590526</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 17 Mar 2007 00:58:34 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hell ho ! </p><p>I want to be able to send some double values over a network, but as I want to do it the right way I am in search of some converting function.</p><p>Have you ever hear about something as ntohd, htond functions ?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (GullRaDriel)</author>
		<pubDate>Tue, 13 Mar 2007 19:29:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>No such function. Doubles are stored in IEEE-754 format on Intel, the same may be true for Macs. This being the case, you can simply transfer them as data.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Tue, 13 Mar 2007 19:33:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>As discussed before, it can often be more efficient to send numbers as text.  It can even be more accurate if the numbers are rational e.g. 0.1
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matt Smith)</author>
		<pubDate>Tue, 13 Mar 2007 19:52:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Not that the gained accuracy means anything, since you&#39;ll just convert it to base 2 when you receive it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Tue, 13 Mar 2007 19:57:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">extern</span> <span class="k1">int</span> StringToNumber<span class="k2">(</span>string MyString<span class="k2">)</span>
<span class="k2">{</span>
  istringstream converter<span class="k2">(</span>MyString<span class="k2">)</span><span class="k2">;</span>
  <span class="k1">int</span> result<span class="k2">;</span>
  converter <span class="k3">&gt;</span><span class="k3">&gt;</span> result<span class="k2">;</span>
  <span class="k1">return</span> result<span class="k2">;</span>
<span class="k2">}</span>
<span class="k1">extern</span> string NumberToString<span class="k2">(</span><span class="k1">int</span> Number<span class="k2">)</span>
<span class="k2">{</span>
  ostringstream converter<span class="k2">;</span>
  converter <span class="k3">&lt;</span><span class="k3">&lt;</span> Number<span class="k2">;</span>
  <span class="k1">return</span> converter.str<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (piccolo)</author>
		<pubDate>Tue, 13 Mar 2007 20:12:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Great, and it has nothing to do with doubles <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Tue, 13 Mar 2007 20:17:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Just use that templated one posted a while back.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Tue, 13 Mar 2007 20:26:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It&#39;s true, but at least you can be sure the other side can convert it to its own format, using strtod() and sprintf()
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matt Smith)</author>
		<pubDate>Tue, 13 Mar 2007 20:28:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks to All, thanks to piccolo too ( even if you were not seeing what the question was ).</p><p>CGamesPlay: I was thinking so but waiting for someone to confirm.<br />Matt Smith: I got big and tons of values. Using sprintf trick is too much cost.</p><p>What I got tested: same byte order in all Intel/Amd. seem reversed for Sun/Aix.</p><p>A good job has been made <a href="http://brlcad.org/doxygen/d7/ded/htond_8c-source.html">there</a></p><p>I am waiting for BAF and his link !</p><p>I want to give cookies !</p><p>Quick !</p><p><img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (GullRaDriel)</author>
		<pubDate>Tue, 13 Mar 2007 21:04:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You could also use Allegro fixed point datatype. It should have guaranteed percision on all platforms. Only problem is limited range and percsision.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Tue, 13 Mar 2007 21:09:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Basically all machines use IEEE-754 floating point format. However, the endianess of this value is like any other issue. What I did for my network library GNE is to say that all values will be sent little-endian (I chose this because I expect virtually all peers to be little-endian). For floating point I said that I will send in little-endian IEEE-754 single or double precision. Well, Intel and Sun and PPC machines all use IEEE-754, so what the plan is just to say machines that don&#39;t support IEEE-754 are just simply &quot;not supported&quot; by any current port of GNE, and if I really need to do it, it isn&#39;t too onerous to actually parse a IEEE-754 number by breaking it into its components then using portable C to &quot;convert&quot; it to a float/double. But since I can&#39;t find a computer you actually need to do this for nor expect any target user of my library to have one, I never wrote the code.</p><p>So, in other words, do whatever you want as long as you precisely define it and give yourself a way out. If that&#39;s true, then if the unexpected happens, you can actually perform the work.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gillius)</author>
		<pubDate>Tue, 13 Mar 2007 22:30:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I don&#39;t recall what thread it was in, but it was something like (untested code I just wrote):</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">template</span><span class="k3">&lt;</span><span class="k1">typename</span> t&gt; t ReadNum<span class="k2">(</span>std::string src<span class="k2">)</span>
<span class="k2">{</span>
    t ret<span class="k2">;</span>
    std::stringstream s<span class="k2">(</span>src<span class="k2">)</span><span class="k2">;</span>
    s <span class="k3">&gt;</span><span class="k3">&gt;</span> ret<span class="k2">;</span>
    <span class="k1">return</span> ret<span class="k2">;</span>
<span class="k2">}</span>

<span class="k1">template</span><span class="k3">&lt;</span><span class="k1">typename</span> t&gt; std::string WriteNum<span class="k2">(</span>t src<span class="k2">)</span>
<span class="k2">{</span>
    std::stringstream ret<span class="k2">;</span>
    s <span class="k3">&lt;</span><span class="k3">&lt;</span> src<span class="k2">;</span>
    <span class="k1">return</span> s.str<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

You would do something like:
</p><div class="source-code snippet"><div class="inner"><pre>string myString <span class="k3">=</span> WriteNum<span class="k2">(</span>myFloat<span class="k2">)</span><span class="k2">;</span>
<span class="c">// then:</span>
<span class="k1">float</span> myFloat <span class="k3">=</span> ReadNum<span class="k3">&lt;</span>float&gt;<span class="k2">(</span>myString<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

Again, untested, but probably works.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Tue, 13 Mar 2007 23:57:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>What gillius said.  Something like:
</p><div class="source-code snippet"><div class="inner"><pre>COMPILE_TIME_ASSERT<span class="k2">(</span><span class="k1">sizeof</span><span class="k2">(</span><span class="k1">float</span><span class="k2">)</span> <span class="k3">=</span><span class="k3">=</span> <span class="k1">sizeof</span><span class="k2">(</span><span class="k1">int</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="k1">float</span> ntohf <span class="k2">(</span> <span class="k1">float</span> net_format <span class="k2">)</span> <span class="k2">{</span>
   <span class="k1">int</span> r <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_598.html" target="_blank">ntohl</a><span class="k2">(</span><span class="k2">(</span><span class="k1">int</span><span class="k3">&amp;</span><span class="k2">)</span>net_format<span class="k2">)</span><span class="k2">;</span>
   <span class="k1">return</span> <span class="k2">(</span><span class="k1">float</span><span class="k3">&amp;</span><span class="k2">)</span>r<span class="k2">;</span>
<span class="k2">}</span>
<span class="k1">float</span> htonf <span class="k2">(</span> <span class="k1">float</span> net_format <span class="k2">)</span> <span class="k2">{</span>
   <span class="k1">int</span> r <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_464.html" target="_blank">htonl</a><span class="k2">(</span><span class="k2">(</span><span class="k1">int</span><span class="k3">&amp;</span><span class="k2">)</span>net_format<span class="k2">)</span><span class="k2">;</span>
   <span class="k1">return</span> <span class="k2">(</span><span class="k1">float</span><span class="k3">&amp;</span><span class="k2">)</span>r<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
That requires that ints be 32 bit types.  There&#39;s no pretty (by pretty I mean, without ifdefs) way to do the same for doubles, because doubles are 64 bit and there&#39;s no standard endianness conversion function for 64 bit values.  </p><p>I do not like the use of text for this purpose.  The advantages are that it is a variable length format that the standard library already supports.  The disadvantages are that it has a space efficiency &lt; 50% and its decimal nature makes it use more cycles than is needed for something as simple as encoding a floating point value.  <br />If you were going to use text-based packets anyway though, why make your packets be stringstreams instead of using ReadNum/WriteNum wrappers?  <br />edit:added missing paren
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (orz)</author>
		<pubDate>Wed, 14 Mar 2007 01:19:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Baf said:</div><div class="quote"><p>

untested, but probably works.
</p></div></div><p>
Heres a more optimized version:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">template</span><span class="k3">&lt;</span><span class="k1">typename</span> t&gt; t ReadNum<span class="k2">(</span>std::string src<span class="k2">)</span>
<span class="k2">{</span>
    t ret<span class="k2">;</span>
    std::istringstream s<span class="k2">(</span>src<span class="k2">)</span><span class="k2">;</span>
    s <span class="k3">&gt;</span><span class="k3">&gt;</span> ret<span class="k2">;</span>
    <span class="k1">return</span> ret<span class="k2">;</span>
<span class="k2">}</span>

<span class="k1">template</span><span class="k3">&lt;</span><span class="k1">typename</span> t&gt; std::string WriteNum<span class="k2">(</span>t src<span class="k2">)</span>
<span class="k2">{</span>
    std::ostringstream ret<span class="k2">;</span>
    s <span class="k3">&lt;</span><span class="k3">&lt;</span> src<span class="k2">;</span>
    <span class="k1">return</span> s.str<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
Probably good to overload a reference parm for ReadNum for cases where you need even less overhead.
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">template</span><span class="k3">&lt;</span><span class="k1">typename</span> t&gt; <span class="k1">void</span> ReadNum<span class="k2">(</span>std::string src, t <span class="k3">&amp;</span>ret<span class="k2">)</span>
<span class="k2">{</span>
    std::istringstream s<span class="k2">(</span>src<span class="k2">)</span><span class="k2">;</span>
    s <span class="k3">&gt;</span><span class="k3">&gt;</span> ret<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Wed, 14 Mar 2007 04:44:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That&#39;s not &quot;more optimized&quot;, it&#39;s just a smaller vtable <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /> A more worthwhile optimization:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">template</span><span class="k3">&lt;</span><span class="k1">typename</span> t&gt; t ReadNum<span class="k2">(</span><span class="k1">const</span> std::string<span class="k3">&amp;</span> src<span class="k2">)</span>
<span class="k2">{</span>
    t ret<span class="k2">;</span>
    std::istringstream s<span class="k2">(</span>src<span class="k2">)</span><span class="k2">;</span>
    s <span class="k3">&gt;</span><span class="k3">&gt;</span> ret<span class="k2">;</span>
    <span class="k1">return</span> ret<span class="k2">;</span>
<span class="k2">}</span>

<span class="k1">template</span><span class="k3">&lt;</span><span class="k1">typename</span> t&gt; std::string WriteNum<span class="k2">(</span><span class="k1">const</span> t<span class="k3">&amp;</span> src<span class="k2">)</span>
<span class="k2">{</span>
    std::ostringstream ret<span class="k2">;</span>
    s <span class="k3">&lt;</span><span class="k3">&lt;</span> src<span class="k2">;</span>
    <span class="k1">return</span> s.str<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Wed, 14 Mar 2007 08:11:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
That&#39;s not &quot;more optimized&quot;, it&#39;s just a smaller vtable
</p></div></div><p>
If it wasn&#39;t more optimized why would it exist?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Wed, 14 Mar 2007 13:04:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, I got my problem resolved:</p><p>On all Intel (PC) computers, nothing to do.</p><p>I test the following:
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="p">#include &lt;stdio.h&gt;</span></td></tr><tr><td class="number">2</td><td>&#160;</td></tr><tr><td class="number">3</td><td><span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span><span class="k2">{</span></td></tr><tr><td class="number">4</td><td>&#160;</td></tr><tr><td class="number">5</td><td>FILE <span class="k3">*</span>out<span class="k2">;</span></td></tr><tr><td class="number">6</td><td>&#160;</td></tr><tr><td class="number">7</td><td><span class="k1">double</span> toto <span class="k3">=</span> <span class="n">3</span>.<span class="n">1415926535</span><span class="k2">;</span></td></tr><tr><td class="number">8</td><td>&#160;</td></tr><tr><td class="number">9</td><td>out <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_337.html" target="_blank">fopen</a><span class="k2">(</span> <span class="s">"test"</span> , <span class="s">"w"</span> <span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td>&#160;</td></tr><tr><td class="number">11</td><td><a href="http://www.delorie.com/djgpp/doc/libc/libc_371.html" target="_blank">fwrite</a><span class="k2">(</span> <span class="k3">&amp;</span>toto, <span class="k1">sizeof</span><span class="k2">(</span> <span class="k1">double</span> <span class="k2">)</span> , <span class="n">1</span> , out <span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">12</td><td>&#160;</td></tr><tr><td class="number">13</td><td><a href="http://www.delorie.com/djgpp/doc/libc/libc_308.html" target="_blank">fclose</a><span class="k2">(</span> out <span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">14</td><td>&#160;</td></tr><tr><td class="number">15</td><td><a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"%d\n"</span>,<span class="k1">sizeof</span><span class="k2">(</span> <span class="k1">double</span><span class="k2">)</span> <span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>&#160;</td></tr><tr><td class="number">17</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

On both the output value is 8 meaning that the size is the same.</p><p>On Intel bytes are ordered as following:
</p><div class="quote_container"><div class="title">od -cx said:</div><div class="quote"><p>

od -cx test<br />0000000   D 027   A   T 373   !  \t   @<br />        1744 5441 21fb 4009<br />0000010
</p></div></div><p>

On SUN this is the following:
</p><div class="quote_container"><div class="title">od -cx said:</div><div class="quote"><p>

0000000   @  \t   ! 373   T   A 027   D<br />            4009    21fb    5441    1744<br />0000010
</p></div></div><p>

<s>So basically we can see that we should swap bytes by pack of two from start to end.</s></p><p>So basically we can see that we should swap bytes from start to end.</p><p><img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" /></p><p>Thanks to all !</p><p>Cookies time !</p><p>EDIT: WTF ! I did as you said in my test code (getting the double value when opening an intel file on a sun ) And I really dunno why I write a so silly mistake. Thanks for pointing it out, orz.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (GullRaDriel)</author>
		<pubDate>Wed, 14 Mar 2007 13:42:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
So basically we can see that we should swap bytes by pack of two from start to end.
</p></div></div><p>
Nope.  You should reverse in units of individual bytes, not sets of 2 bytes.  The reason why you&#39;re seeing it in sets of 2 bytes is because you&#39;re printing them as 2-byte values, and the different endianness then hides the sub-2-byte ordering stuff.  When changing endianness on any modern computer, the correct method is to reverse order in 1 byte units.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (orz)</author>
		<pubDate>Wed, 14 Mar 2007 14:15:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Fixed !
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (GullRaDriel)</author>
		<pubDate>Wed, 14 Mar 2007 15:53:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
If it wasn&#39;t more optimized why would it exist?
</p></div></div><p>If feet aren&#39;t meters why do they exist?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Wed, 14 Mar 2007 18:21:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
If feet aren&#39;t meters why do they exist?
</p></div></div><p>
Because the geniuses who designed C++ didn&#39;t create the foot or the meter.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Wed, 14 Mar 2007 23:31:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Do you actually know the details of how stringstream, istreamstream, and ostringstream work? What about stringbuf?</p><p>stringstream, istringstream, and ostringstream all use stringbuf internally. It&#39;s the same class.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Thu, 15 Mar 2007 00:19:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Do you actually know the details of how stringstream, istreamstream, and ostringstream work? What about stringbuf?
</p></div></div><p>
Yes sir.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Thu, 15 Mar 2007 00:19:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Please either rephrase your original statement, support it, retract it or just stop posting.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Thu, 15 Mar 2007 00:30:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You&#39;ve given me so many options...</p><p>Anyway, my point that supported it fairly solidly was that the designers of C++ (lets make an acronym form them, how about Good Old [C++] DesierS) put a whole lot of thought into every bit of C++.</p><p>If the GODS thought that there should be an istringstream and an ostringstream class as opposed to just a single stringstream class then the GODS must of had a good reason.  <i>I don&#39;t question the GODS&#39; purpose in this and yet I accept that the GODS are all knowing (in the domain of C++) and therefore I embrace and accept the <b>istringstream</b> into my life.</i>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Fri, 16 Mar 2007 23:40:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Fair enough. My point is that istringstream isn&#39;t any <i>faster</i> than stringstream, it simply has a smaller vtable.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Sat, 17 Mar 2007 00:58:34 +0000</pubDate>
	</item>
</rss>
