NSString 与UTF8 的字符转换

时间:2022-09-11 22:44:10

1.由NSString 生成  UTF8 :

NSString ObNSString = @"Testing";

 NSString * encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (CFStringRef)ObNSString, NULL, NULL,  kCFStringEncodingUTF8 ));

 或者使用一下方法:

NSString utf8Str = @"Testing";

NSString *unicodeStr = [NSString stringWithCString:[utf8Str UTF8String] encoding:NSUnicodeStringEncoding];


2.将数据转换成NSString :

NSString *TestString;

NSString *transString = [NSString stringWithString:[TestString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

或者使用以下方法:

NSData *Testdata;

NSString *responseString=[[NSString alloc] initWithData:Testdata encoding:NSUTF8StringEncoding];