将一个字符串分成一个数组

发布于 2024-12-14 15:53:37 字数 782 浏览 0 评论 0原文

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

我的字符串可能看起来有几种不同的方式。以下是示例:

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

我该怎么做?

I have a string of binary numbers coming into my Arduino. I need to convert them into an array. The data represents the columns of light in an LED display. In my program I already have a working function that takes an array and uses the data to display words to the screen. The data needs to be formatted like shown below:

My char string could look a few different ways. Here are the examples:

char CurrentWord = "11111110001000000100011111110000000B00000001000001111111110000010000000";

Or

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

Or even:

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

The above examples would produce the word "Hi" on the screen. In order for the diplay to work however the data must be converted into an array. so it must look like this:

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

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倚栏听风 2024-12-21 15:53:37

如果问题是如何让 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] << ' ';
}

Displays 127 8 8 127 0
请注意,该宏要求所有输入均为零,后跟七个一/零。

如果这不是您的问题,那么我不知道您想问我们什么。

If the question is how to make C++ parse binary, then use this macro:

#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] << ' ';
}

Displays 127 8 8 127 0
Note that this macro requires all inputs to be a zero followed by seven ones/zeros.

If that isn't your question, then I don't know what you're asking of us.

娜些时光,永不杰束 2024-12-21 15:53:37
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
    }
}

从我在 如何阅读的答案中推断来自二进制文件的 bitN 整数数据?
请注意,这不会检查边界,并且会读取超出给定数组末尾的内容。

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

Extrapolated from my answer at How to read bitN integer data from a binary file?
Note this doesn't check boundries and will read past the end of the array given.

贱贱哒 2024-12-21 15:53:37

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

c++ version:

#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;
    }
}
挖鼻大婶 2024-12-21 15:53:37

您可以使用类似这样的内容:

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

You can use something like this:

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

or

char letter_H[5] = {B1111111,
                    B0001000,
                    B0001000,
                    B0001000,
                    B1111111}

char* translate_to_bitarray(char c)
{
  switch(c)
  {
  case 'H':
  case 'h':
   return letter_H;
  ...
  }
}

and then to make a function to translate a whole string...

Advice:
Use a "framebuffer" - an array of 10 x 5 chars or 10 structs.
Send to Arduino, normal, C strings.
Convert characters from string, as needed, and put them in the frame buffer. In this way you can create scroll effect, very easily.
Use a struct to keep character information.

struct character
{
  char _column1;
  char _column2;
  char _column3;
  char _column4;
  char _column5;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文