emoji前端:Unicode与UTF_8的转化

/**

*   Unicode ---> UTF-8

*/

- (NSString*)replaceUnicode:(NSString*)aUnicodeString

{

NSString *tempStr1 = [aUnicodeString 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"];

}

/**

*   utf8 ---> Unicode

*/

-(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];

[s appendFormat:@"\\\\u%x",_char];

}

return s;

}

你可能感兴趣的:(emoji前端:Unicode与UTF_8的转化)