[ENet-discuss] Sending numerical values in packets

Rene Dudfield illumen at yahoo.com
Sat Nov 8 05:26:49 PST 2003


Mika Halttunen wrote:

> Greetings!
> I was wondering, how could I send numerical values, i.e. short ints, 
> ints and floats, using ENetPackets?
>
> I would also need to read them in on the receiver's side of course. 
> And what about big- and little-endianess?
>
> For example: I want to sent player position packet, containing id (= 
> byte) and a pair of floats.
> ------------------------------------------
> char array[128] = "";
> int data_len = 1 + 4 + 4;    // 1 byte, two floats
>
> // Modify the array here to contain the values
> array[0] = PLAYER_POS_ID;    // For example 1
> // ...
>
> ENetPacket *packet = enet_packet_create(array, data_len, 0);
>
> // Send it away
> // ...
> ------------------------------------------


If both your machines are of the same architecture, or use the same 
float format, you can just send the data as is.  Or you could make your 
own function to cast floats into a network format, and another one to 
convert into the machines format.  Another option is to use 
sprintf/sscanf to convert to and from strings containing floats.

The sprintf/sscanf will be larger than necessary but at least easily 
portable.  You do not need to write float2network, network2float 
functions for each platform.

Maybe there is a better way which is portable allready?


Have fun!
http://www.holepit.com/



More information about the ENet-discuss mailing list