The code is here:
NSString * str = [[NSString alloc] initWithFormat:@"abcdefgh"];
void * pbuffer = malloc(128);
memset(pbuffer, 0, 128);
NSUInteger nsu;
NSRange range = NSMakeRange(0, 128);
[str getBytes:pbuffer maxLength:128 usedLength:&nsu encoding:NSUnicodeStringEncoding options:0 range:range remainingRange:nil];
NSString * str2 = [[NSString alloc] initWithBytes:pbuffer length:128 encoding:NSUnicodeStringEncoding];
Then I will see the string str and str2 result would be different.
The buffer is still right, the memory is like “61 00 62 00 ….”.
And the memory in str2 is like “00 61 00 62 ….”.
What’s the problem in my testing code?
Thanks!
Use
NSUTF8StringEncodinginstead ofNSUnicodeStringEncoding