How do you convert an unsigned char array to an NSData in objective c?
This is what I am trying to do, but it doesn't work.
Buffer is my unsigned char array.
NSData *data = [NSData dataWithBytes:message length:length];
You can just use this NSData class method
+ (id)dataWithBytes:(const void *)bytes length:(NSUInteger)length
Something like
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*) [data bytes];
I'm needing to get all of the bytes from an NSData object after the 12th index.
So far I have
const char* fileBytes = (const char*)[imageData bytes];
NSUInteger length = [imageData length];
NSUInteger index;
NSMutableString *byteString = [[NSMutableString alloc] init];
[SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:#"bytes: %#", imageData.description]];
for (index = 12; index<length; index++) { //Grabba returns a 12 byte header.
char aByte = fileBytes[index];
[byteString appendString:[NSString stringWithFormat:#"%x", aByte]];
}
But what I'd really like to do is create a const char* and just append each byte at index 12 till the end to it. How can I append bytes to a series of bytes?
What you need to do is use some standard C library calls. You know how many bytes there are in the NSData object, so you can alloc the storage you need and then memcpy all the bytes from the 12th one on.
NSUInteger length = [data length];
char* theBytes = malloc(sizeof(char)*(length-11));
memcpy(theBytes,fileBytes+11,length-11);
theBytes cannot be a const char* because of course it is not constant until after you have called memcpy.
Remember to call free when you are done with the string.
I am trying to convert NSString to char str[]
i have tried this
NSString *data = #"08052678744default0000000";
char mac [[data length]];
strncpy(mac ,[data UTF8String], [data length]);
But got the wrong result.
Your help is urgently needed in resolving this. Thanks
For example:
NSString *data = #"08052678744default0000000";
const char* utf8String = [data UTF8String];
size_t len = strlen(utf8String) + 1;
char mac [len];
memcpy(mac, utf8String, len);
Note the difference between string length and byte length!
I need somewhere to save and then to retrieve Sint16 (2 bytes).
I get:
SInt16* frames = (SInt16*)inBuffer->mAudioData;
and want to save &frames[i] somewhere (NSMutableData?) that could later easily retrieve. I tried to save like this (in cycle):
[recordedData appendBytes:&frames[i] length:1];
and retrieve:
SInt16* framesRetrieve ;
//sets up mybytes buffer and reads in data from myData
framesRetrieve = (SInt16*)malloc([mutableData framesRetrieve]);
[mutableData getBytes:framesRetrieve];
But this return not the same as i put into.
So what could be a solution ?
thanks.
You need to know the length of the data and then you can store the whole buffer in an NSMutableData object:
To store:
SInt16 *frames = ...;
NSUInteger length = ...; // Assuming number of elements in frames, not bytes!
NSMutableData *data = [[NSMutableData alloc] initWithCapacity:0];
[data appendBytes:(const void *)frames length:length * sizeof(SInt16)];
To retrieve:
SInt16 *frames = (SInt16 *)[data bytes];
NSUInteger length = [data length] / sizeof(SInt16);
You are only copying one byte from a 2 byte variable. Try appending both bytes to the NSMutableData at once.
[recordedData appendBytes: frames length:sizeof(SInt16)];
I have a NSData item that is holding a bunch of ints. How do I go about getting them out and into an NSArray?
The memory structure in the NSData is 32-bit int in little-endian order, one right after the other.
Sorry for the basic question, but still learning the obj-c way of doing things :)
You can use the functions defined in OSByteOrder.h to deal with endianness. Aside from that quirk, this is really just a matter of grabbing the byte buffer and iterating over it.
// returns an NSArray containing NSNumbers from an NSData
// the NSData contains a series of 32-bit little-endian ints
NSArray *arrayFromData(NSData *data) {
void *bytes = [data bytes];
NSMutableArray *ary = [NSMutableArray array];
for (NSUInteger i = 0; i < [data length]; i += sizeof(int32_t)) {
int32_t elem = OSReadLittleInt32(bytes, i);
[ary addObject:[NSNumber numberWithInt:elem]];
}
return ary;
}
Sounds like there are cleaner ways to do what you're trying to do, but this should work:
NSData *data = ...; // Initialized earlier
int *values = [data bytes], cnt = [data length]/sizeof(int);
for (int i = 0; i < cnt; ++i)
NSLog(#"%d\n", values[i]);
This answer is very similar to other answers above, but I found it instructive to play with casting the NSData bytes back to an int32_t[] array. This code works correctly on a little-endian processor (x64 in my case) but would be silently wrong on big-endian (PPC) because the byte representation would be big-endian.
int32_t raw_data[] = {0,1,2,3,4,5,6,7,8,9,10};
printf("raw_data has %d elements\n", sizeof(raw_data)/sizeof(*raw_data));
NSData *data = [NSData dataWithBytes:(void*)raw_data length:sizeof(raw_data)];
printf("data has %d bytes\n", [data length]);
int32_t *int_data_out = (int32_t*) [data bytes];
for (int i=0; i<[data length]/4; ++i)
printf("int %d = %d\n", i, int_data_out[i]);
[data release];
One possible solution below.
To take endianness into account, look up Core Endian Reference in the XCode doc set (you probably would use EndianS32_LtoN (32 bit litte endian to native endianness)).
int mem[]= {0x01, 0x02, 0x03, 0x04, 0xff};
NSData * data = [NSData dataWithBytes:mem length:sizeof(mem)*sizeof(int)];
NSMutableArray * ar = [NSMutableArray arrayWithCapacity:10];
/* read ints out of the data and add them to the array, one at a time */
int idx=0;
for(;idx<[data length]/sizeof(int);idx+=sizeof(int))
[ar addObject:[NSNumber numberWithInt:*(int *)([data bytes] + idx)]];
NSLog(#"Array:%#", ar);