[MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?
Ben Greear
greear at cyberhighway.net
Mon Sep 7 10:40:57 CEST 1998
On Mon, 7 Sep 1998, Adam J. Thornton wrote:
> On Mon, Sep 07, 1998 at 07:52:41AM -0600, T. Alexander Popiel wrote:
> > In message: <Pine.LNX.3.96.980906215952.30075A-100000 at shamen.cyberhighway.net>
> > >What is the standard way (if there is one) to encode floating
> > >point numbers (double too I guess) into bytes for transport accross
> > >the network.
> > >It would seem like there would be a method to do this somewhere!
> > Standard is to send raw IEEE format across the wire; just about
> > everyone uses IEEE internally these days, and those who don't
> > know how to convert, so they can talk with those that do.
>
> You'd clearly use htonl() for ints. You could use fcvt() or ecvt(). Or
> maybe even sprintf(). All of these are probably a little more portable, but
> much slower, and when you're recovering the number with sscanf() you need
> to worry about precision.
I'm perfectly happy sending bits across in IEEE 754 format. However,
I don't know how to get the bits. Can you bit-mask floats? Basically,
I need a way to grab the mantissa and the exponent as either raw bits
or integers. Then I can pack it according to the IEEE standard.
Maybe I'm being too complicated. Maybe something like this would encode:
file_descriptor f; //assume it's connected appropriately.
float f = 42.5;
char* bytes = (char*)(&f);
write(f, bytes, 0, 4); //think those args are right..
This ignores network order, but I can deal with that.
Ben
>
> Adam
> --
> adam at princeton.edu
> "There's a border to somewhere waiting, and a tank full of time." - J. Steinman
>
> --
> MUD-Dev: Advancing an unrealised future.
>
Ben Greear (greear at cyberhighway.net) http://www.primenet.com/~greear
Author of ScryMUD: mud.primenet.com 4444
http://www.primenet.com/~greear/ScryMUD/scry.html
More information about the mud-dev-archive
mailing list