c++ - 在 C++ 中解析缓冲区数据

标签 c++ parsing buffer

我的 C++ 项目有一个缓冲区,可以是任意大小并由蓝牙填充。传入消息的格式类似于 0x43 0x0B 0x00 0x06 0xA2 0x03 0x03 0x00 0x01 0x01 0x0A 0x0B 0x0B 0xE6 0x0D,其中以 0x43 开头,以 0x0D 结尾。所以,这意味着每次缓冲区被填充时,它可以根据上述消息格式有不同顺序的内容。

static const int BufferSize = 1024;
byte buffer[BufferSize];
  1. 解析此缓冲区中传入消息的最佳方法是什么?
  2. 由于我来自 Java 和 .NET,将每个提取的消息作为对象的最佳方法是什么?类可能是解决方案?
  3. 我已经创建了一个单独的类来像下面那样解析缓冲区,我的方向正确吗?

#include<parsingClass.h>
class A
{
   parsingClass ps;
public:
   parsingClass.parse(buffer, BufferSize);
}

最佳答案

class ReturnMessage{
    char *message; 
    public:
    char *getMessage(unsigned char *buffer,int count){
         message = new char[count];
         for(int i = 1; i < count-2; i++){
            message[i-1] = buffer[i];
         }
         message[count-2] = '\0';
         return message;
    }
};
class ParserToMessage{
static int BufferSize = 1024;
unsigned char buffer[BufferSize];
unsigned int counter;
public:
  static char *parse_buffer()
  {
     ReturnMessage rm;
     unsigned char buffByte;
     buffByte = blueToothGetByte(); //fictional getchar() kind of function for bluetooth
     if(buffByte == 0x43){
        buffer[counter++] = buffByte;
        //continue until you find 0x0D
        while((buffByte = blueToothGetByte()) != 0x0D){
            buffer[counter++] = buffByte;
        }
     }
     return rm.getMessage(buffer,counter);
   }
};

关于c++ - 在 C++ 中解析缓冲区数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12954463/

相关文章:

java - 如何从文本文件中删除逗号?

c++ - VBA 中的 "Invalid use of New Keyword"使用用 C++ 编写的旧 com 对象

c++ - C++ 代码片段的大 O 表示法和时间复杂度

java - 如何为输入错误日期的用户正确创建循环?

linux - 提取 pcap 文件中的字节范围

java - 如何解决解析问题?

c++ - 编辑器核心缓冲区类型和语法突出显示

c++ - 在 C++ 中使用 auto 声明 lambda 变量的首选方法是什么?

c++ - 在 C++ 中检测并清除从 dll 导入的错误对象

c# - 带流的文件 I/O - 最佳内存缓冲区大小