一、nsstring转utf-8
NSString * str1=[@"你好" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
二、utf-8转unicode
NSString * title=[self utf8ToUnicode:@"你好"];
- (NSString *) utf8ToUnicode:(NSString *)string
{
NSUInteger length = [string length];
NSMutableString *s = [NSMutableString stringWithCapacity:0];
for (int i = 0;i < length; i++)
{
unichar _char = [string characterAtIndex:i];
//判断是否为英文和数字
if (_char <= ‘9‘ && _char >=‘0‘)
{
[s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i,1)]];
}else if(_char >=‘a‘ && _char <= ‘z‘){
[s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i,1)]];
}else if(_char >=‘A‘ && _char <= ‘Z‘){
[s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i,1)]];
}else{
[s appendFormat:@"\u%x",[string characterAtIndex:i]];
}
}
return s;
}