【问题标题】:Separate a string into an array将一个字符串分隔成一个数组
【发布时间】:2011-12-26 13:00:41
【问题描述】:

我的 Arduino 中有一串二进制数字。我需要将它们转换为数组。数据代表 LED 显示屏中的光列。在我的程序中,我已经有一个工作函数,它接受一个数组并使用数据在屏幕上显示单词。数据需要格式化如下:

我的 char 字符串可能有几种不同的外观。以下是示例:

char CurrentWord = "11111110001000000100011111110000000B00000001000001111111110000010000000";

或者

char CurrentWord = "1111111 0001000 0001000 1111111 0000000 B0000000 1000001 1111111 1000001 0000000";

甚至:

char CurrentWord = "B1111111 B0001000 B0001000 B1111111 B0000000 B0000000 B1000001 B1111111 B1000001 B0000000";

以上示例将在屏幕上生成单词“Hi”。但是,为了使显示正常工作,必须将数据转换为数组。所以它必须看起来像这样:

int CurrentWordInt[] = {B1111111, B0001000, B0001000, B1111111, B0000000, B0000000, B1000001, B1111111, B1000001, B0000000};

我该怎么做?

【问题讨论】:

  • arduino sdk 有 strsep(或 strtok)吗?
  • 应该标记为objective-c吗?
  • 这看起来与您两天前提出的问题相似。也许你应该使用那里的答案! stackoverflow.com/questions/8043482/…
  • 你有 7 位字节,还是只是一个错字?
  • 目标 C 问题在哪里?

标签: c++ c arrays string arduino


【解决方案1】:

如果问题是如何让 C++ 解析二进制,那么使用这个宏:

#define B(in) ( ((in&(1<< 0))>> 0) \
               |((in&(1<< 3))>> 2) \
               |((in&(1<< 6))>> 4) \
               |((in&(1<< 9))>> 6) \
               |((in&(1<<12))>> 8) \
               |((in&(1<<15))>>10) \
               |((in&(1<<18))>>12) \
               |((in&(1<<21))>>14) \
               |((in&(1<<24))>>16) \
              )


#include <iostream>
int main() {
    int CurrentWordInt[] = {B(01111111), B(00001000), B(0001000), B(01111111), B(0000000)}; 
    std::cout << CurrentWordInt[0] << ' ';
    std::cout << CurrentWordInt[1] << ' ';
    std::cout << CurrentWordInt[2] << ' ';
    std::cout << CurrentWordInt[3] << ' ';
    std::cout << CurrentWordInt[4] << ' ';
}

显示 127 8 8 127 0
请注意,此宏要求所有输入都是零,后跟七个 1/0。

如果那不是你的问题,那么我不知道你在问我们什么。

【讨论】:

  • 他必须从字符转换为 5x7 矩阵 LED 显示屏。这是一个矩阵lc-led.com/View/itemNumber/489。每个值为 1 的位表示一个 ON LED。为了显示一个字符,他必须使用 35 位……但那是“不可能的”,他必须使用 40 位。当然,位结构是可能的,但会导致更复杂的代码,无论如何编译器都会为结构分配 40 字节......
【解决方案2】:
class bitbuffer {
    char buffer;
    char held_bits;
    char* input;
public:
    bitbuffer(char* in) :held_bits(0), buffer(0), input(in) {}
    unsigned long long read(unsigned char bits) { 
        unsigned long long result = 0;
        //if the buffer doesn't hold enough bits
        while (bits > held_bits) {
            //grab the all bits in the buffer
            bits -= held_bits;
            result |= ((unsigned long long)buffer) << bits;
            buffer = *(input++);
            held_bits = (char)std::cin.gcount() * CHAR_BIT;
        }
        //append the bits left to the end of the result
        result |= buffer >> (held_bits-bits);
        //remove those bits from the buffer
        held_bits -= bits;
        buffer &= (1ull<<held_bits)-1;
        return result;
    };
};

int main() {
    char* CurrentWord = data from stream
    bitbuffer bitter(CurrentWord);
    int CurrentWordInt[10];
    for(int i=0; i<10; ++i) {
        CurrentWordInt[i] = bitter.read(7); //reads next 7 bits
    }
}

根据我在How to read bitN integer data from a binary file? 的回答推断 请注意,这不会检查边界,并且会读取给定数组的末尾。

【讨论】:

    【解决方案3】:

    c++ 版本:

    #include <iostream>
    #include <string>
    #include <sstream>
    #include <vector>
    
    using namespace std;
    
    int binstrToInt(const char* str){
        int wk = 0;
        while(*str){
            wk = wk * 2 + (*str++ - '0');
        }
        return wk;
    }
    
    vector<int> binstrToVector(const char* str, const size_t size){
        vector<int> v;
        istringstream iss(str);
    
        while(iss){
            ostringstream oss;
            for(int i=0;i<size;){
                char x;
                iss >> x;
                if(x == 'B' || x == 'b') continue;
                oss << x;
                ++i;
            }
            v.push_back(binstrToInt(oss.str().c_str()));
        }
        return v;
    }
    
    int main(){
        const char* binstr = "11111110001000000100011111110000000B00000001000001111111110000010000000";
        vector<int> v = binstrToVector(binstr, 7);
    
        for(int i=0;i<v.size();++i){
            cout << v[i] << endl;
        }
    }
    

    【讨论】:

      【解决方案4】:

      你可以这样使用:

      char alphabet[256][5] = {{B0000000,
                                B0000000,
                                B0000000,
                                B0000000,
                                B0000000},
                                ...
                                //hLetter
                               {B1111111,
                                B0001000,
                                B0001000,
                                B0001000,
                                B1111111},
                                ....
                              };
      

      char letter_H[5] = {B1111111,
                          B0001000,
                          B0001000,
                          B0001000,
                          B1111111}
      
      char* translate_to_bitarray(char c)
      {
        switch(c)
        {
        case 'H':
        case 'h':
         return letter_H;
        ...
        }
      }
      

      然后创建一个函数来翻译整个字符串...

      建议: 使用“帧缓冲区”——一个 10 x 5 字符或 10 个结构的数组。 发送到 Arduino,正常,C 字符串。 根据需要从字符串转换字符,并将它们放入帧缓冲区。通过这种方式,您可以非常轻松地创建滚动效果。 使用结构来保存字符信息。

      struct character
      {
        char _column1;
        char _column2;
        char _column3;
        char _column4;
        char _column5;
      }
      

      【讨论】:

      • 我不认为 C++ 会解析 B1111111
      • 他有一个专门的嵌入式设备编译器。
      猜你喜欢
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      • 2011-04-30
      • 2021-01-31
      • 1970-01-01
      • 2022-10-05
      • 1970-01-01
      • 2018-05-18
      相关资源
      最近更新 更多