将字符串分隔为数组

时间:2011-11-10 21:05:38

标签: c++ c arrays string arduino

我的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”字样。为了使diplay工作,数据必须转换为数组。所以看起来应该是这样的:

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

我该怎么做?

4 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

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

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

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

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

答案 1 :(得分:0)

如果问题是如何使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。

如果不是您的问题,那么我不知道您对我们的要求。

答案 2 :(得分:0)

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 :(得分:0)

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;
    }
}
相关问题