由于系统键盘中的表情不得不在发表之前转码,于是度娘后找到了下面两种比较好用的方法,记录下来以便以后使用。
1、Unicode转汉字
#pragma mark unicode转汉字
+ (NSString *)replaceUnicode:(NSString *)unicodeStr {
NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData
mutabilityOption:NSPropertyListImmutable
format:NULL
errorDescription:NULL];
return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"];
}
2、汉字转换为Unicode
#pragma mark 汉子转Unicode
+ (NSString *) utf8ToUnicode:(NSString *)string
{
NSUInteger length = [string length];
NSMutableString *uniStr = [NSMutableString stringWithCapacity:0];
for (int i = 0;i < length; i++)
{
unichar _char = [string characterAtIndex:i];
//判断是否为英文和数字
if (_char <= '9' && _char >= '0')
{
[uniStr appendFormat:@"%@",[string substringWithRange:NSMakeRange(i, 1)]];
}
else if(_char >= 'a' && _char <= 'z')
{
[uniStr appendFormat:@"%@",[string substringWithRange:NSMakeRange(i, 1)]];
}
else if(_char >= 'A' && _char <= 'Z')
{
[uniStr appendFormat:@"%@",[string substringWithRange:NSMakeRange(i, 1)]];
}
else
{
[uniStr appendFormat:@"\\u%x",[string characterAtIndex:i]];
}
}
return uniStr;
}