[Solved] NSData to NSArray of UInt16 in Objective C [closed]


You can’t get NSArray with UInt16 elements because NSArray can contain only NSObjects, so you have to wrap UInt16 to NSNumber or use some other container (for example, simple c array). The closest code will be something like this:

// Just preparing some test data
NSMutableData *data = [NSMutableData new];
for (int i = 0; i<10; ++i){
    UInt16 u = (UInt16) arc4random()%UINT16_MAX;
    NSLog(@"%d",u);
    [data appendBytes:&u length:sizeof(u)];
}

// the main code
UInt16 *adr = (UInt16 *)data.bytes;
NSMutableArray *arr = [NSMutableArray new];
for (int i = 0; i<data.length/sizeof(UInt16); ++i) {
    NSNumber *num = [NSNumber numberWithUnsignedShort:*adr];
    ++adr;
    [arr addObject:num];
}

solved NSData to NSArray of UInt16 in Objective C [closed]