diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lisiblestd/bytes.c | 24 | ||||
| -rw-r--r-- | src/lisiblestd/bytes.h | 11 |
2 files changed, 35 insertions, 0 deletions
diff --git a/src/lisiblestd/bytes.c b/src/lisiblestd/bytes.c new file mode 100644 index 0000000..0860800 --- /dev/null +++ b/src/lisiblestd/bytes.c @@ -0,0 +1,24 @@ +#include "bytes.h" +#include "assert.h" +#include <string.h> + +u32 u32_from_bytes(const u8 *bytes) { + LSTD_ASSERT(bytes != NULL); + return (bytes[0] << 24) + (bytes[1] << 16) + (bytes[2] << 8) + bytes[3]; +} +u32 u32_from_bytes_le(const u8 *bytes) { + LSTD_ASSERT(bytes != NULL); + return (bytes[3] << 24) + (bytes[2] << 16) + (bytes[1] << 8) + bytes[0]; +} + +u16 u16_from_bytes_le(const u8 *bytes) { + LSTD_ASSERT(bytes != NULL); + return (bytes[1] << 8) + bytes[0]; +} + +float float_from_bytes_le(const u8 *bytes) { + LSTD_ASSERT(bytes != NULL); + float result; + memcpy(&result, bytes, sizeof(float)); + return result; +} diff --git a/src/lisiblestd/bytes.h b/src/lisiblestd/bytes.h new file mode 100644 index 0000000..8af109b --- /dev/null +++ b/src/lisiblestd/bytes.h @@ -0,0 +1,11 @@ +#ifndef LSTD_BYTES_H +#define LSTD_BYTES_H + +#include "types.h" + +u32 u32_from_bytes(const u8 *bytes); +u32 u32_from_bytes_le(const u8 *bytes); +u16 u16_from_bytes_le(const u8 *bytes); +float float_from_bytes_le(const u8 *bytes); + +#endif // LSTD_BYTES_H |
