Archive

Archive for February, 2009

Convert Java Float into C/C++ Float

February 12th, 2009 No comments

If you have a float stored using writeFloat use the following to read it back into C

float aFloat;
unsigned char *pBytes;
pBytes = (unsigned char * )&aFloat;
pBytes[3] = _buffer[_offset++];
pBytes[2] = _buffer[_offset++];
pBytes[1] = _buffer[_offset++];
pBytes[0] = _buffer[_offset++];
return aFloat;

Assume that _buffer is your byte array or pointer to an unsigned char *, and that _offset is the offset in your data.

To read an integer value:

int i = ((_buffer[_offset]<<24)|(_buffer[_offset+1]<<16)|(_buffer[_offset+2]<<8)|(_buffer[_offset+3]));
_offset+=4;
return i;

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
Categories: Uncategorized, XCode / C Tags: