如何在C中存储字节数组?

OJF*_*ord 1 c pebble-watch pebble-sdk

这似乎是一个基本问题,但我无法在任何地方找到答案.

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

我试图在我的Android应用程序中传输12个传输,每个传输96个字节在Pebble上.

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

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

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
}
Run Code Online (Sandbox Code Playgroud)

我甚至远程关闭?

bro*_*oot 5

您可以分配12*96字节的连续内存,包含12行和96列

char*   image = (char*)malloc(sizeof(char)*12*96);
Run Code Online (Sandbox Code Playgroud)

另外,全局数组也可以

char image[12][96];
Run Code Online (Sandbox Code Playgroud)

根据我的理解,您正在逐行接收数据,即一次96字节:

char rcvd_data[96]={0};
Run Code Online (Sandbox Code Playgroud)

和访问/设置像这样::

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 
    }
}
Run Code Online (Sandbox Code Playgroud)

并一次性传输所有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);
}
Run Code Online (Sandbox Code Playgroud)