Skip to content

Commit 111b181

Browse files
yonschnashif
authored andcommitted
byteorder: Add macros for converting ints to arrays
Add three macros: sys_uint{16,32,64}_to_array, to convert integers to byte arrays in a byte order aware manner. For example, sys_uint16_to_array(0x0123) evaluates to: {0x01, 0x23} for big endian machines, and {0x23, 0x01} for little endian machines. Signed-off-by: Yonatan Schachter <[email protected]>
1 parent 6e188d9 commit 111b181

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

include/zephyr/sys/byteorder.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,49 @@
168168
* @return 48-bit integer in big-endian format.
169169
*/
170170

171+
/** @def sys_uint16_to_array
172+
* @brief Convert 16-bit unsigned integer to byte array.
173+
*
174+
* @details Byte order aware macro to treat an unsigned integer
175+
* as an array, rather than an integer literal. For example,
176+
* `0x0123` would be converted to `{0x01, 0x23}` for big endian
177+
* machines, and `{0x23, 0x01}` for little endian machines.
178+
*
179+
* @param val 16-bit unsigned integer.
180+
*
181+
* @return 16-bit unsigned integer as byte array.
182+
*/
183+
184+
/** @def sys_uint32_to_array
185+
* @brief Convert 32-bit unsigned integer to byte array.
186+
*
187+
* @details Byte order aware macro to treat an unsigned integer
188+
* as an array, rather than an integer literal. For example,
189+
* `0x01234567` would be converted to `{0x01, 0x23, 0x45, 0x67}`
190+
* for big endian machines, and `{0x67, 0x45, 0x23, 0x01}` for
191+
* little endian machines.
192+
*
193+
* @param val 32-bit unsigned integer.
194+
*
195+
* @return 32-bit unsigned integer as byte array.
196+
*/
197+
198+
/** @def sys_uint64_to_array
199+
* @brief Convert 64-bit unsigned integer to byte array.
200+
*
201+
* @details Byte order aware macro to treat an unsigned integer
202+
* as an array, rather than an integer literal. For example,
203+
* `0x0123456789abcdef` would be converted to
204+
* `{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}`
205+
* for big endian machines, and
206+
* `{0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01}` for
207+
* little endian machines.
208+
*
209+
* @param val 64-bit unsigned integer.
210+
*
211+
* @return 64-bit unsigned integer as byte array.
212+
*/
213+
171214
#ifdef CONFIG_LITTLE_ENDIAN
172215
#define sys_le16_to_cpu(val) (val)
173216
#define sys_cpu_to_le16(val) (val)
@@ -189,6 +232,27 @@
189232
#define sys_cpu_to_be48(val) __bswap_48(val)
190233
#define sys_be64_to_cpu(val) __bswap_64(val)
191234
#define sys_cpu_to_be64(val) __bswap_64(val)
235+
236+
#define sys_uint16_to_array(val) { \
237+
((val) & 0xff), \
238+
(((val) >> 8) & 0xff)}
239+
240+
#define sys_uint32_to_array(val) { \
241+
((val) & 0xff), \
242+
(((val) >> 8) & 0xff), \
243+
(((val) >> 16) & 0xff), \
244+
(((val) >> 24) & 0xff)}
245+
246+
#define sys_uint64_to_array(val) { \
247+
((val) & 0xff), \
248+
(((val) >> 8) & 0xff), \
249+
(((val) >> 16) & 0xff), \
250+
(((val) >> 24) & 0xff), \
251+
(((val) >> 32) & 0xff), \
252+
(((val) >> 40) & 0xff), \
253+
(((val) >> 48) & 0xff), \
254+
(((val) >> 56) & 0xff)}
255+
192256
#else
193257
#define sys_le16_to_cpu(val) __bswap_16(val)
194258
#define sys_cpu_to_le16(val) __bswap_16(val)
@@ -210,6 +274,27 @@
210274
#define sys_cpu_to_be48(val) (val)
211275
#define sys_be64_to_cpu(val) (val)
212276
#define sys_cpu_to_be64(val) (val)
277+
278+
#define sys_uint16_to_array(val) { \
279+
(((val) >> 8) & 0xff), \
280+
((val) & 0xff)}
281+
282+
#define sys_uint32_to_array(val) { \
283+
(((val) >> 24) & 0xff), \
284+
(((val) >> 16) & 0xff), \
285+
(((val) >> 8) & 0xff), \
286+
((val) & 0xff)}
287+
288+
#define sys_uint64_to_array(val) { \
289+
(((val) >> 56) & 0xff), \
290+
(((val) >> 48) & 0xff), \
291+
(((val) >> 40) & 0xff), \
292+
(((val) >> 32) & 0xff), \
293+
(((val) >> 24) & 0xff), \
294+
(((val) >> 16) & 0xff), \
295+
(((val) >> 8) & 0xff), \
296+
((val) & 0xff)}
297+
213298
#endif
214299

215300
/**

0 commit comments

Comments
 (0)