MRAS
Multi Rocket Avionics System
Loading...
Searching...
No Matches
serializers.h
1//
2// Created by Tom Danvers on 04/02/2023.
3//
4
5#ifndef MRAS_SERIALIZERS_H
6#define MRAS_SERIALIZERS_H
7
8#include <stddef.h>
9#include "stdint.h"
10
17template<typename input_type>
18void toByteArray(uint8_t* outputByteArray, input_type inputData) {
19 // create a pointer to the first byte of the struct in memory
20 auto* startPtr = (uint8_t*)(&inputData);
21
22 // increment the memory address for each byte in the struct and shove it into a byte array
23 for (size_t i = 0; i < sizeof(inputData); i++) {
24 outputByteArray[i] = *(startPtr + i);
25 }
26};
27
34template<typename output_type>
35output_type fromByteArray(const uint8_t* byteArray) {
36 return *((output_type*) byteArray);
37};
38
39#endif //MRAS_SERIALIZERS_H