I have a string that I want as an array of bytes. So far I have used NSData for this:
NSString *message = @"testing"; NSData *messageData = [message dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:YES]; NSUInteger dataLength = [messageData length]; Byte *byteData = (Byte*)malloc( dataLength ); memcpy( byteData, [messageData bytes], dataLength );
But I know that NSString has a getBytes:maxLength:usedLength:encoding:options:range:remainingRange: , which will let me skip everything together using NSData. My problem is that I do not know how to set all the parameters correctly.
I assume that the accepted array of pointers should be malloc'ed, but I'm not sure how to find how much memory there is for malloc. I know that there are [NSString lengthOfBytesUsingEncoding:] and [NSString maximumLengthOfBytesUsingEncoding:] , but I don’t know if those methods are needed and I don’t quite understand the difference between them. I assume this will be the same value as for maxLength . Other parameters make sense from the documentation. Any help would be great. Thanks.
Brian source share