Getting weird characters when going from NSString

2019-08-06 13:09发布

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.

2条回答
放我归山
2楼-- · 2019-08-06 13:54

You want something like this?

    NSString *message = @"testing";    
    NSData *bytes = [message dataUsingEncoding:NSUTF8StringEncoding];
    NSString* messageDecoded = [[NSString alloc] initWithData:bytes encoding:NSUTF8StringEncoding];
    NSLog(@"decoded: %@", messageDecoded);
查看更多
男人必须洒脱
3楼-- · 2019-08-06 14:02

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.

查看更多
登录 后发表回答