问题
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
NSStringEncodingConversionExternalRepresentationto theoptions:parameter ingetBytes: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