Need Alternative to the below Swift code in Objective-C
let arr = data.withUnsafeBytes {
Array(UnsafeBufferPointer<UInt16>(start: $0, count: data.count/MemoryLayout<UInt16>.stride))
}
Thanks for your help, the below answer helped, had to do this in C++ as was working on tensorflow-lite pre-processing.
UInt16 *adr = (UInt16 *)data.bytes;
uint16_t req_arr[data.length/sizeof(UInt16)];
for (int i = 0; i<data.length/sizeof(UInt16); ++i) {
uint16_t num16 = *adr%UINT16_MAX;
++adr;
your_arr[i] = num16;
}
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];
}