Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过网络发送布尔数组 我尝试用C++上的WebSu套来发送一个二维布尔数组。 websocket中有一个“data”结构,它看起来像这样: /** * Data structure for message */ struct Data { Data():bytes(nullptr), len(0), issued(0), isBinary(false){} char* bytes; ssize_t len, issued; bool isBinary; };_C++_Networking_Websocket_Cocos2d X - Fatal编程技术网

通过网络发送布尔数组 我尝试用C++上的WebSu套来发送一个二维布尔数组。 websocket中有一个“data”结构,它看起来像这样: /** * Data structure for message */ struct Data { Data():bytes(nullptr), len(0), issued(0), isBinary(false){} char* bytes; ssize_t len, issued; bool isBinary; };

通过网络发送布尔数组 我尝试用C++上的WebSu套来发送一个二维布尔数组。 websocket中有一个“data”结构,它看起来像这样: /** * Data structure for message */ struct Data { Data():bytes(nullptr), len(0), issued(0), isBinary(false){} char* bytes; ssize_t len, issued; bool isBinary; };,c++,networking,websocket,cocos2d-x,C++,Networking,Websocket,Cocos2d X,在这里,我想使用上面的结构发送以下数据包 bool[12][19] info; 这个数组中有228个布尔值,我正在考虑将整个信息复制到char数组,即数据中的字节。 这将产生228个字符值 我想我可以这样做,但我觉得效率不高。 有更好的方法吗 使用位。但是您应该像散列一样维护索引。所以一个字节可以表示8位布尔值。对于您的情况,您可能需要全部字符[228/8+1]。请将其存储在位图中。如果您知道二维数组的尺寸,那么很容易找到相应的一维数组的长度。一个 char 代表了大多数C++实现(当然是CO

在这里,我想使用上面的结构发送以下数据包

bool[12][19] info;
这个数组中有228个布尔值,我正在考虑将整个信息复制到char数组,即数据中的
字节。
这将产生228个字符值

我想我可以这样做,但我觉得效率不高。

有更好的方法吗

使用位。但是您应该像散列一样维护索引。所以一个字节可以表示8位布尔值。对于您的情况,您可能需要全部字符[228/8+1]。请将其存储在位图中。如果您知道二维数组的尺寸,那么很容易找到相应的一维数组的长度。一个<代码> char 代表了大多数C++实现(当然是COCOS2D-X支持的所有)的8位存储。 类似这样的内容说明了基本思想:

#include <assert.h>
static const size_t char_bits = 8;
static const size_t serializedSize = (12*19+(char_bits-1))/(char_bits);

class ByteArraySerialize
{
protected:

void serialize(char* dst, bool src[12][19]){
  for(int x=0; x<12; x++){
    for(int y=0; y<19; y++){
      bool b = src[x][y];
      int i=x*19+y;
      assert(i < serializedSize*char_bits);
      int i_char = i / char_bits;
      int i_bit = i % char_bits;
      if(b)
        dst[i_char] |= 1 << i_bit;
      else
        dst[i_char] &= ~(1 << i_bit);
    }
  }
}

void deserialize(bool dst[12][19], const char* src){
  for(int x=0; x<12; x++){
    for(int y=0; y<19; y++){
      int i=x*19+y;
      assert(i < serializedSize*char_bits);
      int i_char = i / char_bits;
      int i_bit = i % char_bits;
      bool b = ((src[i_char] >> i_bit) & 0x01) != 0;
      dst[x][y] = b;
    }
  }
}

public:
ByteArraySerialize(){
  char charbuf[serializedSize];
  bool data_1[12][19] = { 0 };
  bool data_2[12][19] = { 0 };
  for(int x=0; x<12; x++)
    for(int y=0; y<19; y++)
      data_1[x][y] = rand()%1!=0;
  serialize(charbuf,data_1);
  deserialize(data_2, charbuf);

  for(int x=0; x<12; x++)
    for(int y=0; y<19; y++)
      assert(data_1[x][y] == data_2[x][y]);
}
};

ByteArraySerialize testByteArray;
#包括
静态常量大小字符位=8;
静态常量size_t serialized=(12*19+(字符位-1))/(字符位);
类ByteArraySerialize
{
受保护的:
无效序列化(char*dst,bool src[12][19]){

对于(intx=0;xWait,你能给我举个例子吗?我仍然很困惑,因为我必须使用字符数组来发送信息。你是指字符[228/8+1]?是的,就像std::vector的实现一样