1

这似乎是一个基本问题,但我在 SO 的任何地方都找不到答案。

我知道 C/C++ 没有byte数据类型。我知道sizeof(char) == 1

我试图在 Pebble 上存储 12 个传输,每个 96 个字节,从我的 Android 应用程序传输。

由于传输大小的限制,我一次发送一个。每个都应该“附加”到最后一个,因为它们最终应该在内存中形成顺序空间,以作为图像读取(每像素一位)。

我正在尝试做这样的事情:

int transNum = 0;
uint8_t image[][] = new uint8t[12][12] //not sure about uint8_t, and I've forgotten how to do 'new' in C, I have to create just a pointer, and then malloc?

receivedHandler(DictionaryIterator *iter, void *context){
    Tuple *receivedImage = dict_find(iter, KEY_IMG);
    for (int i = 0; i < 12; i++) {
        image[transNum][i] =  receivedImage->value[i]->uint8_t;
    }
    transNum += 1; //this is in an implicit loop, since once done Pebble ACKs the transmission, and receivedHandler is called again by the next transmission
}

我什至离得很近吗?

4

4 回答 4

5

您可以分配具有 12 行和 96 列的 12*96 字节的连续内存

char*   image = (char*)malloc(sizeof(char)*12*96);

此外,一个全局数组就可以了

char image[12][96];

据我了解,您正在逐行接收数据,即一次 96 个字节:

char rcvd_data[96]={0};

并像这样访问/设置::

for(row=0;row<12;row++) //to point to the row (0-11 rows)
{
   rcvd_data= recv_function(); //whatever your recv function is
   for(col=0;col<96;col++) //to point to the col in that row (0-95 col)
   {
      *(image + row*96 + col)= rcvd_data[col]//whatever value you want to assign 
    }
}

并一次性传输所有 96 个字节:

for(row=0;row<12;row++) //to point to the row (0-11 rows)
{
   rcvd_data= recv_function(); //whatever your recv function is
   memcopy((image + row*96), rcvd_data, 96);
}
于 2014-03-07T02:36:58.760 回答
2

我要补充的一件事是小心使用char. 在处理纯字节数据时使用unsigned char,signed char或之类的东西。uint8_t虽然 char 是一个字节,但我已经看到由于它的使用而丢失了数据。

于 2014-03-07T03:29:58.297 回答
1

分配您描述的数组的最简单解决方案是:

uint8_t image[12][96];

但是,根据您的描述“每个都应该'附加'到最后一个,因为它们最终应该在内存中形成顺序空间”,这表明您实际上想要:

uint8_t image[12 * 96];

然后您只需将您的 12 传输顺序写入该数组。

你写的这段代码:

for (int i = 0; i < 12; i++) {
    image[byteNum][i] =  receivedImage->value[i]->uint8_t;
}

不对,uint8_t是数据类型,不是字段名。

此外,您可能想用 做一些事情image[i],而不是image[byteNum][i]虽然我不能更具体,但不知道它TupleType是什么以及您希望在每个 中找到多少Tuple

于 2014-03-07T03:10:57.097 回答
0

我认为你需要 malloc 两次。

char** image = (char**)malloc(sizeof(char*)*12);
int i = 0;
for(; i < 12; ++i){
   image[i] = (char*)malloc(sizeof(char)*12);
}

然后,您可以将图像视为二维数组。

于 2014-03-07T02:57:46.290 回答