Codeamateur
Codeamateur

Reputation: 21

How to convert float array to byte array in C++ (Arduino)

How to convert array of floats to array of bytes in Arduino. Basically convert all float variables of the array to bytes (and round all variables to the nearest integral value) in an efficient way.

Convert this:

float mlx90640To[768];

to:

byte bytearray[768];

Upvotes: 1

Views: 575

Answers (1)

A M
A M

Reputation: 15265

I guess that I maybe do not fully understand the question. It maybe an XY-Problem.

But a fast and efficient solution could be the below.

#include <iostream>
#include <cmath>

using byte = unsigned char;

float mlx90640To[768];
byte bytearray[768];

int main() {

    // Convert all float values
    for (size_t i{}; i < 768; ++i)
        bytearray[i] = static_cast<byte>(std::lround(mlx90640To[i]));

    return 0;
}

Upvotes: 1

Related Questions