2011-12-16 186 views
0

我有如下所示的二維數組。如何獲取二維數組中的char數組元素

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))]; 
+1

既然你聲明瞭一個48字節數組的數組,那就是你得到的。如果它沒有被外部代碼使用,你可以使用一個包含長度的結構? – 2011-12-16 06:46:23

回答

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 。如果沒有,你必須提供一個額外的數組記錄每行的字節數。

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

^_^

相關問題