Getting weird characters when going from NSString to bytes and then back to NSString

吃可爱长大的小学妹 提交于 2019-12-11 07:16:40

问题


NSString *message = @"testing";    
NSUInteger dataLength = [message lengthOfBytesUsingEncoding:NSUnicodeStringEncoding];
void *byteData = malloc( dataLength );
NSRange range = NSMakeRange(0, [message length]);
NSUInteger actualLength = 0;
NSRange remain;
BOOL result =   [message getBytes:byteData maxLength:dataLength usedLength:&actualLength encoding:NSUnicodeStringEncoding options:0 range:range remainingRange:&remain];
NSString *decodedString = [[NSString alloc] initWithBytes:byteData length:actualLength encoding:NSUnicodeStringEncoding];

My issue is that I expect decodedString to be testing, but instead it looks like chinese characters. I thought it could be an issue with null-terminated data, but it seems that that should not be an issue.


回答1:


The UTF-16 byte order is getting reversed between the encode and decode.

You can do any one of the following:

  • Use an encoding that specifies an explicit byte order (e.g., NSUTF16BigEndianStringEncoding, NSUTF16LittleEndianStringEncoding, NSUTF8StringEncoding).

  • Pass NSStringEncodingConversionExternalRepresentation to the options: parameter in getBytes:maxLength:usedLength:encoding:options:range:. This prepends a byte-order mark to the start of the data.

  • Use NSData, as Elvis suggested.

These days, UTF-8 is the preferred Unicode encoding in most cases.




回答2:


You want something like this?

    NSString *message = @"testing";    
    NSData *bytes = [message dataUsingEncoding:NSUTF8StringEncoding];
    NSString* messageDecoded = [[NSString alloc] initWithData:bytes encoding:NSUTF8StringEncoding];
    NSLog(@"decoded: %@", messageDecoded);


来源:https://stackoverflow.com/questions/8021926/getting-weird-characters-when-going-from-nsstring-to-bytes-and-then-back-to-nsst

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!