[SDL] [SDL_net] Endianness

Oleg mybrokenbeat at gmail.com
Wed Mar 20 12:29:25 PDT 2013


	You should pack(when sending)\unpack(when recving) all your integers in any of endianness (by your choice). For example(pseudo code):

    uint32_t GetBe32(const uint8_t *data)
    {
        uint32_t val;
        val =  GetBe16(data) << 16;
        val |= GetBe16(&data[2]);
        return val;
    }

    void PutBe32(uint8_t* data,uint32_t val)
    {
          data[0] = val >> 24;
          data[1] = val >> 16;
          data[2] = val >> 8;
          data[3] = val;
    }
...
// When you send:
    uint32_t value = 123456;
    
    PutBe32(data,value);
    SDLNet_TCP_Send(sock,data,size);
...
// When you recv:
    uint32_t value;

    size = SDLNet_TCP_Recv(sock, data, maxlen);
    if (size > 4)
	    value = GetBe32(data);
    else
        goto error;

20.03.2013, в 17:07, Glocke написал(а):

> Hi, do I have to consider byte orders while working with TCPsocket in case of SDLNet_TCP_Recv and SDLNet_TCP_Send. If yes: what's the way to handle endianness in this case?
> 
> Kind regards
> Glocke
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20130320/429d210f/attachment-0009.htm>


More information about the SDL mailing list