Append to const char* bytes? - objective-c

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.

Related

Wrap bytes data

I need to wrap a string into a sequence of bytes according to a pattern:
First byte should be 0x02
The second byte and the third byte should be the length of the string in bytes
Then the string sequence of bytes should come
Next byte should be 0x03
Last byte should be the logical XOR of the bytes sequence of the string
This is what I tried to do:
- (NSData *)sendMessage:(NSData *)message {
Byte stx = 0x02;
Byte etx = 0x03;
Byte length = [message length];
// Computes bitwise XOR of message bytes
const char *bytes = [message bytes];
Byte crc = bytes[0];
for (int i = 1; i < [message length]; i++)
crc = crc ^ bytes[i];
NSString *packet = [NSString stringWithFormat:#"%x%x%s%x%x", stx, (uint16_t)length, bytes, etx, crc];
return [packet dataUsingEncoding:NSUTF8Encoding];
}
I need to write this bytes sequence into a CBCharacteristic that gets then read by a BT device, checks the format and, if it's correct, display the string. Anyway, it keeps rejecting it. Could someone please explain me where do I fail?
The problem is the use of stringWithFormat to build the data. That string and its resulting UTF-8 encoding is not at all what you need.
Use NSMutableData to add the bytes:
NSMutableData *result = [[NSMutableData alloc] init];
[result appendBytes:&stx length:1];
uint16_t len = (uint16_t)length;
[result appendBytes:&len length:2]; // might have a byte ordering issue here
[result appendData:message];
[result appendBytes:&crc length:1];

Convert one NSArray filled with NSStrings to a UTF8String vector

So I was wondering, is there some quick way of converting one NSArray filled with NSStrings to the equivalent UTF8string values?
I want to store some parameter configuration in a NSArray and then use them in a function that takes (int argv, const char *argv[]) as arguments.
I implemented this in a convoluted way
int argc = [gameParameters count];
const char **argv = (const char **)malloc(sizeof(const char*)*argc);
for (int i = 0; i < argc; i++) {
argv[i] = (const char *)malloc(sizeof(char)*[[gameParameters objectAtIndex:i] length]+1);
strncpy((void *)argv[i], [[gameParameters objectAtIndex:i] UTF8String], [[gameParameters objectAtIndex:i] length]+1);
}
but I'm not really happy with and cleaning up memory is tedious.
Do you know a better way to achieve this result?
Your current implementation is not correct if the string contains non-ASCII characters. For example, the string #"é" (SMALL LETTER E WITH ACUTE) has length 1, but the UTF-8 sequence "C3 A9" has 2 bytes. Your code would not allocate enough memory for that string.
(In other words: [string length] returns the number of Unicode characters in the string, not the number of bytes of the UTF-8 representation.)
Using strdup(), as suggested by Kevin Ballard, would solve this problem:
argv[i] = strdup([[gameParameters objectAtIndex:i] UTF8String]);
But you should also check if duplicating the strings is necessary at all. If you call the function in the current autorelease context, the following would be sufficient:
int argc = [gameParameters count];
const char **argv = (const char **)malloc(sizeof(const char*)*argc);
for (int i = 0; i < argc; i++) {
argv[i] = [[gameParameters objectAtIndex:i] UTF8String];
}
yourFunction(argc, argv);
free(argv);

Iterate through NSData bytes

How can I iterate through [NSData bytes] one by one and append them to an NSMutableString or print them using NSLog()?
Rather than appending bytes to a mutable string, create a string using the data:
// Be sure to use the right encoding:
NSString *result = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];
If you really want to loop through the bytes:
NSMutableString *result = [NSMutableString string];
const char *bytes = [myData bytes];
for (int i = 0; i < [myData length]; i++)
{
[result appendFormat:#"%02hhx", (unsigned char)bytes[i]];
}
Update! Since iOS 7, there's a new, preferred way to iterate through all of the bytes in an NSData object.
Because an NSData can now be composed of multiple disjoint byte array chunks under the hood, calling [NSData bytes] can sometimes be memory-inefficient, because it needs to flatten all of the underlying chunks into a single byte array for the caller.
To avoid this behavior, it's better to enumerate bytes using the enumerateByteRangesUsingBlock: method of NSData, which will return ranges of the existing underlying chunks, which you can access directly without needing to generate any new array structures. Of course, you'll need to be careful not to go poking around inappropriately in the provided C-style array.
NSMutableString* resultAsHexBytes = [NSMutableString string];
[data enumerateByteRangesUsingBlock:^(const void *bytes,
NSRange byteRange,
BOOL *stop) {
//To print raw byte values as hex
for (NSUInteger i = 0; i < byteRange.length; ++i) {
[resultAsHexBytes appendFormat:#"%02x", ((uint8_t*)bytes)[i]];
}
}];

Obj-C How to convert NSData to an array of ints?

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);

how convert [NSData length] to a NSData

ex:
NSData *data = [NSData dataWithContentsOfFile:filePath];
int len = [data length];
if len = 10000,
i hope i can convert 1000 to a NSData look like
char hoperesult[] = {0x10, 0x27, 0x00, 0x00}
and hoperesult[] must always 4 Bytes
So you want the length in 4 little-endian bytes, correct? I think this will do it:
unsigned int len = [data length];
uint32_t little = (uint32_t)NSSwapHostIntToLittle(len);
NSData *byteData = [NSData dataWithBytes:&little length:4];
(Note that most network protocols use big-endian, but you showed little-endian so that's what this does.)
I'm not 100% sure what you mean here, but I think you are attempting to fill hoperesult with the values found in the file at 'filePath'.
struct _hoperesult {
char data[4];
} *hoperesult;
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSUInteger len = [data length];
NSRange offset;
offset.location = 0;
offset.length = sizeof(_hoperesult);
NSData *hoperesultData;
while( (offset.location + offset.length) < len ) {
hoperesultData = [data subdataWithRange:offset];
// the equivalent of your char hoperesult[] content...
hoperesult = [hoperesultData bytes]
}
An instance of NSData can return a pointer to the actual bytes of data using the "bytes" method. It returns a (const void *). You could in theory simply cast [data bytes] to a char * and use the offset directly; or you can do like in the above code and return smaller chucks of NSData.
Hope that helps!