Byte array utils
Conversion between byte arrays and 16/32-bit integers
Generates 16/32-bit integers from byte arrays, or generates byte arrays from 16/32-bit integers.
Reading
Retrieve uint16_t or uint32_t values from a byte array interpreted as uint8_t in big-endian order.
inline uint8_t G_BYTE(const uint8_t*& p) {
return *(p)++;
}
inline uint16_t G_WORD(const uint8_t*& p) {
uint32_t r = *p++;
r = (r << 8) + *p++;
return r;
}
inline uint32_t G_DWORD(const uint8_t*& p) {
uint32_t r = *p++;
r = (r << 8) + *p++;
r = (r << 8) + *p++;
r = (r << 8) + *p++;
return r;
}
p is incremented by the number of bytes read.
Writing
Writes uint8_t, uint16_t, or uint32_t values in big-endian order to the byte array pointed to by q.
inline uint8_t& S_OCTET(uint8_t*& q, uint8_t c) {
*q++ = c;
return *q;
}
inline uint8_t& S_WORD(uint8_t*& q, uint16_t c) {
*(q) = ((c) >> 8) & 0xff; (q)++;
*(q) = ((c) & 0xff); (q)++;
return *q;
}
inline uint8_t& S_DWORD(uint8_t*& q, uint32_t c) {
*(q) = ((c) >> 24) & 0xff; (q)++;
*(q) = ((c) >> 16) & 0xff; (q)++;
*(q) = ((c) >> 8) & 0xff; (q)++;
*(q) = ((c) & 0xff); (q)++;
return *q;
}
q is incremented by the number of bytes written.
Background
These utilities simplify operations during the construction and decomposition of data payloads in wireless packets.
You may also use the simplified pack_bytes() and expand_bytes() functions.