简体   繁体   中英

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];

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM