Convert unsigned char array to NSData and back Convert unsigned char array to NSData and back objective-c objective-c

Convert unsigned char array to NSData and back


You can just use this NSData class method

+ (id)dataWithBytes:(const void *)bytes length:(NSUInteger)length

Something like

NSUInteger size = // some sizeunsigned char array[size];NSData* data = [NSData dataWithBytes:(const void *)array length:sizeof(unsigned char)*size];

You can then get the array back like this (if you know that it is the right data type)

NSUInteger size = [data length] / sizeof(unsigned char);unsigned char* array = (unsigned char*) [data bytes];