How to convert NSData to byte array in iPhone?

I want to convert NSData to an array of bytes, so I will write the following code:

 NSData *data = [NSData dataWithContentsOfFile:filePath]; int len = [data length]; Byte byteData[len]; byteData = [data bytes]; 

But the error message "Incompatible types at assignment" appears on the last line of code. What is the correct way to convert data to a byte array?

+42
type-conversion objective-c iphone bytearray
Apr 7 '09 at 3:59
source share
5 answers

You cannot declare an array using a variable, therefore Byte byteData[len]; will not work. If you want to copy data from a pointer, you will also need memcpy (which will go through the data pointed to by the pointer and copy each byte to the specified length).

Try:

 NSData *data = [NSData dataWithContentsOfFile:filePath]; NSUInteger len = [data length]; Byte *byteData = (Byte*)malloc(len); memcpy(byteData, [data bytes], len); 

This code will dynamically allocate the array to the correct size (you should free(byteData) when you're done) and copy the bytes into it.

You can also use getBytes:length: as indicated by others if you want to use a fixed-length array. This avoids malloc / free, but is less extensible and more prone to buffer overflow problems, so I rarely use it.

+56
Apr 07 '09 at 6:39
source share

You can also just use bytes where they are and give them the type you need.

 unsigned char *bytePtr = (unsigned char *)[data bytes]; 
+41
Dec 03 '10 at 20:09
source share

Already answered, but summarized to help other readers:

  //Here: NSData * fileData; uint8_t * bytePtr = (uint8_t * )[fileData bytes]; // Here, For getting individual bytes from fileData, uint8_t is used. // You may choose any other data type per your need, eg. uint16, int32, char, uchar, ... . // Make sure, fileData has atleast number of bytes that a single byte chunk would need. eg. for int32, fileData length must be > 4 bytes. Makes sense ? // Now, if you want to access whole data (fileData) as an array of uint8_t NSInteger totalData = [fileData length] / sizeof(uint8_t); for (int i = 0 ; i < totalData; i ++) { NSLog(@"data byte chunk : %x", bytePtr[i]); } 
+11
Apr 21 '14 at 19:03
source share

The signature -[NSData bytes] is - (const void *)bytes . You cannot assign a pointer to an array on the stack. If you want to copy the buffer managed by the NSData object to an array, use -[NSData getBytes:] . If you want to do this without copying, then do not allocate an array; just declare a pointer variable and let NSData manage the memory for you.

+9
Apr 07 '09 at 4:05
source share

This is because the return type for [data bytes] is an array of void * c-style, not Uint8 (which is a byte for typedef).

The error is that you are trying to set the selected array when return is a pointer type, what you are looking for is getBytes: length: call, which will look like this:

 [data getBytes:&byteData length:len]; 

Which fills the array that you allocated with data from the NSData object.

+2
Apr 7 '09 at 4:07
source share



All Articles