Cocoa Framework中GB2312与UTF16编码之间的相互转换
代码如下:
NSString *orgStr = @"你好,世界!"; NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000); NSData *gb2312StrData = [orgStr dataUsingEncoding:enc]; NSString *dummyStr = [NSString stringWithUTF8String:[gb2312StrData bytes]]; NSString *utf16Str = [[NSString alloc] initWithData:gb2312StrData encoding:enc]; NSLog(@"UTF16 string: %@", utf16Str); [utf16Str release]; NSLog(@"Dummy string is: %@", dummyStr);
上述代码片段中,dummyStr仅用于测试,这个对象最后结果为空。