Adding sys_htol() functions for converting from host- to little endian
byte order.
This commit is contained in:
Alfred E. Heggestad 2011-09-06 16:29:46 +00:00
parent 4af64c9612
commit 92887e9713
2 changed files with 42 additions and 0 deletions

View file

@ -59,6 +59,8 @@ int sys_coredump_set(bool enable);
int sys_daemon(void);
uint16_t sys_htols(uint16_t v);
uint32_t sys_htoll(uint32_t v);
uint16_t sys_ltohs(uint16_t v);
uint32_t sys_ltohl(uint32_t v);
uint64_t sys_htonll(uint64_t v);

View file

@ -13,6 +13,46 @@
*/
/**
* Convert a 16-bit value from host order to little endian
*
* @param v 16-bit in host order
*
* @return 16-bit little endian value
*/
uint16_t sys_htols(uint16_t v)
{
uint8_t *p = (uint8_t *)&v;
uint16_t l = 0;
l |= (uint16_t)*p++ << 0;
l |= (uint16_t)*p << 8;
return l;
}
/**
* Convert a 32-bit value from host order to little endian
*
* @param v 32-bit in host order
*
* @return 32-bit little endian value
*/
uint32_t sys_htoll(uint32_t v)
{
uint8_t *p = (uint8_t *)&v;
uint32_t l = 0;
l |= (uint32_t)*p++ << 0;
l |= (uint32_t)*p++ << 8;
l |= (uint32_t)*p++ << 16;
l |= (uint32_t)*p << 24;
return l;
}
/**
* Convert a 16-bit value from little endian to host order
*