MUD-Dev
mailing list archive
[ Other Periods
| Other mailing lists
| Search
]
Date:
[ Previous
| Next
]
Thread:
[ Previous
| Next
]
Index:
[ Author
| Date
| Thread
]
[MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?
Ben Greear wrote:
>
>
> Maybe I'm being too complicated. Maybe something like this would encode:
Yeah.
> 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.
(but your compiler can't deal with multiple f variables :^)
typedef unsigned long uint32;
typedef unsigned char uint8;
assert(sizeof(float)==4);
assert(sizeof(uint32)==4);
assert(char is 8 bit);
assert(floats use same endianess as ints);
void conv(const float f,uint8 *dst){
const uint32 i = *((uint32 *)(void *)&f);
dst[0] = (uint8)((i>>24)&0xff);
dst[1] = (uint8)((i>>16)&0xff);
dst[2] = (uint8)((i>>8)&0xff);
dst[3] = (uint8)((i)&0xff);
}
Does Java provide functions for converting floats to raw bytes?
--
Ola Fosheim Groestad,Norway
- Thread context:
- [MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?, (continued)
- [MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?,
T. Alexander Popiel popiel#snugharbor,com, Mon 07 Sep 1998, 14:53 GMT
- [MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?,
Adam J. Thornton adam#phoenix,Princeton.EDU, Mon 07 Sep 1998, 16:06 GMT
- [MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?,
Ben Greear greear#cyberhighway,net, Mon 07 Sep 1998, 17:43 GMT
- [MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?,
Richard Woolcock KaVir#dial,pipex.com, Mon 07 Sep 1998, 18:07 GMT
- [MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?,
Ola Fosheim Grøstad olag#ifi,uio.no, Mon 07 Sep 1998, 20:44 GMT
- [MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?,
Adam Wiggins adam#angel,com, Tue 08 Sep 1998, 19:26 GMT
[ Other Periods
| Other mailing lists
| Search
]