3 * Copyright (c) 2006-2009 by Roland Riegel <feedback@roland-riegel.de>
5 * This file is free software; you can redistribute it and/or modify
6 * it under the terms of either the GNU General Public License version 2
7 * or the GNU Lesser General Public License version 2.1, both as
8 * published by the Free Software Foundation.
11 #ifndef BYTEORDERING_H
12 #define BYTEORDERING_H
22 * \addtogroup byteordering
28 * Byte-order handling header (license: GPLv2 or LGPLv2.1)
30 * \author Roland Riegel
36 * Converts a 16-bit integer to little-endian byte order.
38 * Use this macro for compile time constants only. For variable values
39 * use the function htol16() instead. This saves code size.
41 * \param[in] val A 16-bit integer in host byte order.
42 * \returns The given 16-bit integer converted to little-endian byte order.
47 * Converts a 32-bit integer to little-endian byte order.
49 * Use this macro for compile time constants only. For variable values
50 * use the function htol32() instead. This saves code size.
52 * \param[in] val A 32-bit integer in host byte order.
53 * \returns The given 32-bit integer converted to little-endian byte order.
57 #define HTOL16(val) (val)
58 #define HTOL32(val) (val)
59 #elif defined(BIG_ENDIAN)
60 #define HTOL16(val) ((((uint16_t) (val)) << 8) | \
61 (((uint16_t) (val)) >> 8) \
63 #define HTOL32(val) (((((uint32_t) (val)) & 0x000000ff) << 24) | \
64 ((((uint32_t) (val)) & 0x0000ff00) << 8) | \
65 ((((uint32_t) (val)) & 0x00ff0000) >> 8) | \
66 ((((uint32_t) (val)) & 0xff000000) >> 24) \
69 #error "Endianess undefined! Please define LITTLE_ENDIAN=1 or BIG_ENDIAN=1."
72 uint16_t htol16(uint16_t h);
73 uint32_t htol32(uint32_t h);
76 * Converts a 16-bit integer to host byte order.
78 * Use this macro for compile time constants only. For variable values
79 * use the function ltoh16() instead. This saves code size.
81 * \param[in] val A 16-bit integer in little-endian byte order.
82 * \returns The given 16-bit integer converted to host byte order.
84 #define LTOH16(val) HTOL16(val)
87 * Converts a 32-bit integer to host byte order.
89 * Use this macro for compile time constants only. For variable values
90 * use the function ltoh32() instead. This saves code size.
92 * \param[in] val A 32-bit integer in little-endian byte order.
93 * \returns The given 32-bit integer converted to host byte order.
95 #define LTOH32(val) HTOL32(val)
98 * Converts a 16-bit integer to host byte order.
100 * Use this function on variable values instead of the
101 * macro LTOH16(). This saves code size.
103 * \param[in] l A 16-bit integer in little-endian byte order.
104 * \returns The given 16-bit integer converted to host byte order.
106 #define ltoh16(l) htol16(l)
109 * Converts a 32-bit integer to host byte order.
111 * Use this function on variable values instead of the
112 * macro LTOH32(). This saves code size.
114 * \param[in] l A 32-bit integer in little-endian byte order.
115 * \returns The given 32-bit integer converted to host byte order.
117 #define ltoh32(l) htol32(l)
125 #define htol16(h) (h)
126 #define htol32(h) (h)
128 uint16_t htol16(uint16_t h);
129 uint32_t htol32(uint32_t h);