Skip to content

Instantly share code, notes, and snippets.

@Gro-Tsen
Created May 23, 2019 19:02
Show Gist options
  • Save Gro-Tsen/e323220f67e20f9724fae41ff85d6fb1 to your computer and use it in GitHub Desktop.
Save Gro-Tsen/e323220f67e20f9724fae41ff85d6fb1 to your computer and use it in GitHub Desktop.
// This code is in the Public Domain.
#include <stdint.h>
#include <string.h>
#include <assert.h>
void
serialize_be_16 (uint8_t b[2], uint16_t w)
{
for ( int i=0 ; i<2 ; i++ )
b[i] = (w>>((2-1-i)*8))&0xff;
}
void
serialize_le_16 (uint8_t b[2], uint16_t w)
{
for ( int i=0 ; i<2 ; i++ )
b[i] = (w>>(i*8))&0xff;
}
void
serialize_mem_16 (uint8_t b[2], uint16_t w)
{
assert (sizeof(uint8_t[2]) == sizeof(uint16_t));
memmove (b, &w, sizeof(uint16_t));
}
void
deserialize_be_16 (uint16_t *wp, const uint8_t b[2])
{
uint16_t w = 0;
for ( int i=0 ; i<2 ; i++ )
w |= ((uint16_t)(b[i]))<<((2-1-i)*8);
*wp = w;
}
void
deserialize_le_16 (uint16_t *wp, const uint8_t b[2])
{
uint16_t w = 0;
for ( int i=0 ; i<2 ; i++ )
w |= ((uint16_t)(b[i]))<<(i*8);
*wp = w;
}
void
deserialize_mem_16 (uint16_t *wp, const uint8_t b[2])
{
assert (sizeof(uint8_t[2]) == sizeof(uint16_t));
memmove (wp, b, sizeof(uint16_t));
}
void
serialize_be_32 (uint8_t b[4], uint32_t w)
{
for ( int i=0 ; i<4 ; i++ )
b[i] = (w>>((4-1-i)*8))&0xff;
}
void
serialize_le_32 (uint8_t b[4], uint32_t w)
{
for ( int i=0 ; i<4 ; i++ )
b[i] = (w>>(i*8))&0xff;
}
void
serialize_mem_32 (uint8_t b[4], uint32_t w)
{
assert (sizeof(uint8_t[4]) == sizeof(uint32_t));
memmove (b, &w, sizeof(uint32_t));
}
void
deserialize_be_32 (uint32_t *wp, const uint8_t b[4])
{
uint32_t w = 0;
for ( int i=0 ; i<4 ; i++ )
w |= ((uint32_t)(b[i]))<<((4-1-i)*8);
*wp = w;
}
void
deserialize_le_32 (uint32_t *wp, const uint8_t b[4])
{
uint32_t w = 0;
for ( int i=0 ; i<4 ; i++ )
w |= ((uint32_t)(b[i]))<<(i*8);
*wp = w;
}
void
deserialize_mem_32 (uint32_t *wp, const uint8_t b[4])
{
assert (sizeof(uint8_t[4]) == sizeof(uint32_t));
memmove (wp, b, sizeof(uint32_t));
}
void
serialize_be_64 (uint8_t b[8], uint64_t w)
{
for ( int i=0 ; i<8 ; i++ )
b[i] = (w>>((8-1-i)*8))&0xff;
}
void
serialize_le_64 (uint8_t b[8], uint64_t w)
{
for ( int i=0 ; i<8 ; i++ )
b[i] = (w>>(i*8))&0xff;
}
void
serialize_mem_64 (uint8_t b[8], uint64_t w)
{
assert (sizeof(uint8_t[8]) == sizeof(uint64_t));
memmove (b, &w, sizeof(uint64_t));
}
void
deserialize_be_64 (uint64_t *wp, const uint8_t b[8])
{
uint64_t w = 0;
for ( int i=0 ; i<8 ; i++ )
w |= ((uint64_t)(b[i]))<<((8-1-i)*8);
*wp = w;
}
void
deserialize_le_64 (uint64_t *wp, const uint8_t b[8])
{
uint64_t w = 0;
for ( int i=0 ; i<8 ; i++ )
w |= ((uint64_t)(b[i]))<<(i*8);
*wp = w;
}
void
deserialize_mem_64 (uint64_t *wp, const uint8_t b[8])
{
assert (sizeof(uint8_t[8]) == sizeof(uint64_t));
memmove (wp, b, sizeof(uint64_t));
}
uint16_t
my_htons (uint16_t hs)
{
uint8_t b[2];
serialize_be_16 (b, hs);
uint16_t ns;
deserialize_mem_16 (&ns, b);
return ns;
}
uint16_t
my_ntohs (uint16_t ns)
{
uint8_t b[2];
serialize_mem_16 (b, ns);
uint16_t hs;
deserialize_be_16 (&hs, b);
return hs;
}
uint32_t
my_htonl (uint32_t hw)
{
uint8_t b[4];
serialize_be_32 (b, hw);
uint32_t nw;
deserialize_mem_32 (&nw, b);
return nw;
}
uint32_t
my_ntohl (uint32_t nw)
{
uint8_t b[4];
serialize_mem_32 (b, nw);
uint32_t hw;
deserialize_be_32 (&hw, b);
return hw;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment