简体   繁体   English

使用 arduino/c/c++ 中的字节序操作为 24 位字节数组重新排序字节顺序?

[英]Reorder byte order for an array of 24-bit bytes using endian manipulations in arduino/c/c++?

I have a 24-bit array:我有一个 24 位数组:

uint32_t rgbdata[] = { 0x00ff00 0xff0000 0x0000ff ...... }

Let's say the above data is in RGB order and I want GRB order假设上面的数据是 RGB 顺序,我想要 GRB 顺序

uint32_t grbdata[] = { 0xff0000 0x00ff00 0x0000ff ...... }

Without using loops, is there a quick endian manipulation way to do a certain byte order?在不使用循环的情况下,是否有一种快速的字节序操作方式来执行特定的字节顺序? Speed is of utmost importance in this case.在这种情况下,速度至关重要。

Here is a partial example of a uint24_t that you might port to your tools (I don't know the 'current' arduino's tool set)这是一个 uint24_t 的部分示例,您可以将其移植到您的工具中(我不知道“当前”arduino 的工具集)

// Note: compile with -std=c++17 for the using comma list
//       or remove these and put the "std::" into code
#include <algorithm>
using std::swap;

#include <iostream>
using std::cout, std::cerr, std::endl, std::hex, std::dec, std::cin; // c++17

#include <string>
using std::string, std::to_string; // c++17

#include <sstream>
using std::stringstream;


class Uint24_t
{
public:
   Uint24_t() : data {0,0,0}
      {
         cout << "\n  sizeof(Uint24_t)= " << sizeof(Uint24_t) // reports 3 bytes
              << " bytes, * 8= " << (sizeof(Uint24_t) * 8) << " bits." << endl;
      }

   Uint24_t(uint32_t initVal) : data {0,0,0}
      {
         data[0] = static_cast<uint8_t>((initVal >>  0) & 0xff); // lsbyte
         data[1] = static_cast<uint8_t>((initVal >>  8) & 0xff); //
         data[2] = static_cast<uint8_t>((initVal >> 16) & 0xff); // msbyte
         cout << "\n  sizeof(Uint24_t)= " << sizeof(Uint24_t) // reports 3 bytes
              << " bytes, * 8= " << (sizeof(Uint24_t) * 8) << " bits." << endl;
      }

   ~Uint24_t() = default;

   std::string show() {
      stringstream ss;
      ss << "  show(): "
         << static_cast<char>(data[2]) << "."
         << static_cast<char>(data[1]) << "."
         << static_cast<char>(data[0]);
      return ss.str();
   }

   std::string dump() {
      stringstream ss;
      ss << "  dump(): " << hex
         << static_cast<int>(data[2]) << "."
         << static_cast<int>(data[1]) << "."
         << static_cast<int>(data[0]);
      return ss.str();
   }

   void swap0_2() { swap(data[0], data[2]); }


private:
   uint8_t  data[3]; // 3 uint8_t 's
};



class T976_t // ctor and dtor compiler provided defaults
{
public:
   int operator()() { return exec(); } // functor entry

private: // methods

   int exec()
      {
         Uint24_t  u24(('c' << 16) +  // msbyte
                       ('b' <<  8) +
                       ('a' <<  0));

         cout << "\n  sizeof(u24) = " << sizeof(u24) << " bytes"
              << "\n  " << u24.show()
              << "\n  " << u24.dump() << std::endl;

         u24.swap0_2(); // swapping lsByte and msByte

         cout << "\n  sizeof(u24) = " << sizeof(u24) << " bytes"
              << "\n  " << u24.show()
              << "\n  " << u24.dump() << std::endl;

         return 0;
      }



}; // class T976_t


int main(int , char**) { return T976_t()(); } // call functor

Typical output:典型输出:

sizeof(Uint24_t)= 3 bytes, * 8= 24 bits.

  sizeof(u24) = 3 bytes
    show(): c.b.a
    dump(): 63.62.61

  sizeof(u24) = 3 bytes
    show(): a.b.c
    dump(): 61.62.63

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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