如何在C中存储字节数组? [英] How to store byte arrays in C?

查看:351
本文介绍了如何在C中存储字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This seems such a basic question, but I cannot find an answer anywhere on SO.

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

I know that C/C++ do not have a byte datatype. I know that sizeof(char) == 1.

我正在尝试将12个传输(每个96个字节)存储在Pebble中,这些传输是从我的Android应用程序传输的.

I'm trying to store 12 transmissions, each of 96 bytes on Pebble, as transmitted from my Android app.

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

Due to limitation in the transmission size, I'm sending one at a time. Each one should be 'appended' to the last, as they should end up forming sequential space in memory, for reading as an image (one bit per pixel).

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

I'm trying to do something like this:

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
}

我甚至可以远程关闭吗?

Am I even remotely close?

推荐答案

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

You can allocate 12*96 byte of contiguous memory having 12 rows & 96 columns

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

另外,全局数组也可以

char image[12][96];

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

From what I understand, you are recieving data row wise ie 96 bytes at a time:

char rcvd_data[96]={0};

并按以下方式访问/设置::

and access/set like this::

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个字节:

and to transfer all 96 bytes in one go:

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);
}

这篇关于如何在C中存储字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆