Objective c iOS上的NSString initWithBytes结果错误
代码如下:Objective c iOS上的NSString initWithBytes结果错误,objective-c,ios,nsstring,Objective C,Ios,Nsstring,代码如下: 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:&
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];
然后我会看到字符串str和str2的结果会有所不同。
缓冲区仍然正确,内存类似于“61 00 62 00…”。
str2中的内存类似于“00610062…”
我的测试代码有什么问题
谢谢 使用
NSUTF8StringEncoding
而不是NSUTF8StringEncoding
谢谢。但是为什么呢?主要原因是什么?因为C字符串使用8位字符,而nsunicoding
使用16位字符。。。