如何在二维数组中获取char数组元素

时间:2011-12-16 06:38:04

标签: objective-c c multidimensional-array char nsdata

我有如下的二维数组。

unsigned char myArray[][48] = {
    {0xfc,0x94,0x88,0x48,0x5f,0xa4,0x9a,0xfb,0x6e,0xf8,0xcd,0x01,0x47,0x64,0x03,0xd0,0x1f,0xb8,0xa3,0x85,0x84,0xa9,0x4a,0xc4,0x9e,0xea,0x26,0x09,0x62,0x96,0x91,0xa6},
    {0xa9,0xc5,0x9a,0xb3,0x09,0x38,0x15,0xb3,0x22,0xb3,0x07,0x21,0x3e,0x39,0x35,0xc6,0x69,0x6e,0xf3,0x64,0xb0,0x0a,0x4c,0xcb,0x77,0xff,0x76,0x3c,0x37,0xf3,0x99,0x96},
    {0x24,0x4d,0xc0,0x45,0xe0,0x50,0x1f,0x72,0x0f,0xb0,0xcc,0xb9,0xc6,0x72,0xa9,0x5a,0xf3,0x5a,0xd9,0xe2,0xc3,0x44,0xd9,0x25,0xf3,0x12,0x6a,0x0c,0x37,0x6a,0x3f,0xb6},
    {0xd3,0x91,0x98,0xfd,0xcd,0x6e,0x01,0x44,0xfc,0xf7,0x5d,0x08,0xab,0xbc,0x43,0xab,0xd3,0x4a,0xd9,0x07,0xa7,0x8e,0xda,0xba,0xb5,0x8a,0x27,0xe1,0xc6,0x7a,0xfe,0xee},
    {0x5f,0x08,0xcc,0x01,0x17,0xde,0xbb,0x99,0x19,0xf3,0x0d,0xd7,0x08,0x5f,0xcd,0xe3,0xe0,0x24,0x54,0xee,0x16,0x68,0xe1,0x64,0x41,0x58,0x09,0xd8,0xf7,0x93,0x8f,0xf1,0xf0,0x00,0xd7,0xd4,0x5b,0x6b,0x8d,0x1e,0x18,0x06,0x1f,0x70,0x6a,0x40,0x86,0x38}
};

当我得到myArray [0]时,我们看到它不是36个字节;相反,它是48.我怎么能得到36个字节,就像我们在数组中看到的那样,没有指定它的大小36,而是得到它。

{0xfc,0x94,0x88,0x48,0x5f,0xa4,0x9a,0xfb,0x6e,0xf8,0xcd,0x01,0x47,0x64,0x3,0xd0,0x1f,0xb8,0xa3,0x85,0x84,0xa9,0x4a,0xc4,0x9e,0xea,0x26,0x09,0x62,0x96,0x91,0xa6}

NSData *row1 = [NSData dataWithBytes:url[0] 
                              length:(sizeof(myArray[0])/sizeof(unsigned char))];

2 个答案:

答案 0 :(得分:0)

NSData *row1 = [NSData dataWithBytes:myArray[0] length:36];

^ _ ^

答案 1 :(得分:0)

前4行只有32个字节。

由于未提供的值初始化为0,您可以向后搜索最后一个非零值,如:

unsigned char* data = myArray[n];
size_t len = sizeof(myArray[n]);
for (; len > 0; -- len)
  if (data[len-1] != 0)
    break;
NSData* row = [NSData dataWithBytes:data length:len];

这假设在您提供的32字节序列的末尾没有0。如果没有,则必须提供一个额外的数组,记录每行的字节数。