Skip to main content
GameDev.net gamedev.net
🔒 Locked

FLOAT in CHAR[32]

Started by int01h Jan 10, 2007 at 5:39 AM 10 replies 2.8k views
Original Post
int01h
int01h
Hi guys, since today my position was sent using INT type of data : char packet[10]; packet[0]=PACKET_GO; packet[1]=player[x].id packet[2]=player[x].position_x; // (int position_x;) packet[3]=player[x].position_y; // (int position_y;) send(...) Any chance to send this way a float value ? With best regards!
CTar
CTar
Is this C or C++? Will the machine receiving this have another endianess or floating point representation? If not then it's basically a matter of splitting a float up into sizeof(float) pieces.

EDIT: In C++, fairly generic functions would look like this:
// NOTE: It's the caller's responsibility to make sure// out_iter can be incremeneted at least sizeof(InT) times.template<typename InT, typename OutputIterator>void split(InT in,OutputIterator out_iter){	char * in_iter = reinterpret_cast<char*>(∈);	char * in_end = in_iter + sizeof(InT);	for( ;in_iter != in_end; ++in_iter, ++out_iter )	{		*out_iter = *in_iter; 	}}// NOTE: It's the caller's responsibility to make sure// in_iter can be incremeneted at least sizeof(OutT) times.template<typename InputIterator, typename OutT>void assemble(InputIterator in_iter, OutT& out){	char * out_iter = reinterpret_cast<char*>(&out);	char * out_end = out_iter + sizeof(OutT);	for( ;out_iter != out_end; ++in_iter, ++out_iter)	{		*out_iter = *in_iter;	}}


And a simple test:
#include <cassert>#include <algorithm>int main(){	float in = 53.33F;	char out[sizeof(float)];	std::fill_n( out, sizeof(float), 0);	split(in,out);	float recieved = 0.0F;	assemble( out, recieved);		assert( recieved == in );	return 0;}
int01h
int01h
This is a C code i work on, Server - C, client C.

Skizz
Skizz
Quote:
Original post by int01h
char packet[10];
packet[0]=PACKET_GO;
packet[1]=player[x].id
packet[2]=player[x].position_x; // (int position_x;)
packet[3]=player[x].position_y; // (int position_y;)
send(...)


The char type is not the same as the int type, they are different sizes. A char can hold the values 0-255 (or -128 to +127)1 whereas an int can hold a much greater range. You are losing significant bits of your player position, unless the possible values are in the range of a char type. This should create a warning when you compile it.

Skizz

1. Assuming 8 bit chars of course.
Ozymandias42
Ozymandias42
Quote:
Original post by Anonymous Poster
float readfloat(unsigned char* buf){assert(buf);float f;memcpy(&f,buf,sizeof(float);return f;}


unrelated -Wall -ansi -pedantic alert!

This is not C. You have to put you assert under your float declaration.

/-pedantic

Sorry, continue discussion.
Ozymandias42
Ozymandias42
C99 allows it, but -pedantic by itself will certainly give you a warning if you don't explicitly specify C99.

Still, I did technically overstate when I said it wasn't C, but within the quotes of exactly and only (-Wall -pedantic -ansi) it would in fact fail. Unless perhaps newer versions of gcc default to C99?

Anyhoo, I've gone way offtopic.
murtzi
murtzi
How about using an union here:

struct MyPacket_s{	int    mp_id;	float  mp_position_x;	float  mp_position_y;};union Convert_u{	struct MyPacket_s  c_packet;	unsigned char      c_data[sizeof (struct MyPacket_s)];};


And then use it like this:

int main( int argc, char *argv[] ){	union Convert_u c;	int   i;	c.c_packet.mp_id = 1;	c.c_packet.mp_position_x = 5.0;	c.c_packet.mp_position_y = 5.0;	for (i=0; i<sizeof (struct MyPacket_s); i++)	{		printf( "0x%02x\n", c.c_data );	}	return 0;}


Of course, this wouldn't work if you have to care about endianess.

GPxz
GPxz
You could use pointers too

float *temp;temp=(float *)&packet[1];*temp=player[x].position_x;



Plus your code is really wrong, youre assigning a INT(>1 byte) value to a CHAR value (1 byte)


PS: How i put this code window?
My MSN: gerunbaum@hotmail.com
hplus0603
hplus0603
The Forum FAQ talks about this. Both in the "how do I send a struct" part, and in the "how do I quantize a float" part.

Btw: to get source display use 'source' tags in brackets.
enum Bool { True, False, FileNotFound };

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.