这似乎是一个基本的问题,但我在任何地方都找不到答案。
我知道C/C++没有byte 数据类型。我知道sizeof(char) == 1**.**
我试图存储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
}我离得很近吗?
发布于 2014-03-07 02:36:58
您可以分配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-07 03:29:58
我要补充的一件事是,只使用char时要小心。在处理纯字节数据时使用类似于unsigned char、signed char或uint8_t的方法。虽然char是一个字节,但由于它的使用,我看到了数据的丢失。
发布于 2014-03-07 03:10:57
分配您描述的数组的最简单的解决方案是:
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中找到多少。
https://stackoverflow.com/questions/22240267
复制相似问题