Search code examples
objective-cnsdatauint8t

Why the size of uint8_t is 8 Byte after convert from 20Byte NSData in Objective-C?


I am developing in ios objective-c BLE , I receive the BLE data and print by NSLog like the following:

NSData *initialData = [characteristic value];
NSData *startOfFrameData = [initialData subdataWithRange:NSMakeRange(0, 20)];
NSLog(@"StartOfFrameData: %@", startOfFrameData);

The data in the log is like the following:

StartOfFrameData: <04621281 00931012 fed0d499 e92ceaac 3286513e>

But it only has 8 Byte after I convert it to the uint8_t via the following code:

uint8_t *tempData1=(uint8_t *)[startOfFrameData bytes];
NSLog(@"before tempData1:%lu", sizeof(tempData1));
for(int i = 0;i<sizeof(tempData1);i++){
   NSLog(@"tempData1[%d] = %x , %d",i,tempData1[i],tempData1[i]);
}

The log show like the following:

tempData1:8
tempData1[0] = f4 , 244
tempData1[1] = 9b , 155
tempData1[2] = b2 , 178
tempData1[3] = 81 , 129
tempData1[4] = 0 , 0
tempData1[5] = 6 , 6
tempData1[6] = 5 , 5
tempData1[7] = 71 , 113

Why it only has 8 Byte after covert to uint_8 ?

Did I missing something? Thanks in advance.


Solution

  • In your for loop you are asking for the size of tempData1. tempData1 is of type uint_8t * which is a pointer. Pointers contain memory addresses and in this case the memory address is 8 bytes.

    You should probably iterate up to startOfFrameData.length.

    For example:

    uint8_t *tempData1 = (uint8_t *)[startOfFrameData bytes];
    
    for(int i = 0; i < startOfFrameData.length; i++){
       NSLog(@"tempData1[%d] = %x , %d",i,tempData1[i],tempData1[i]);
    }