Tuesday, April 23, 2013

NSData dataWithBytes example ios


dataWithBytes :length:

Creates and returns a data object containing a given number of bytes copied from a given buffer.
+ (id)dataWithBytes:(const void *)bytes length:(NSUInteger)length
Parameters
bytes
A buffer containing data for the new object.
length
The number of bytes to copy from bytes. This value must not exceed the length of bytes.
Return Value of [NSData dataWithBytes]
A data object containing length bytes copied from the buffer bytes. Returns nil if the data object could not be created.
Example of [NSData dataWithBytes]
NSUInteger size = // some size
unsigned 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*) [NSData bytes];
Example of [NSData dataWithBytes]