iOS Emoji表情編碼/解碼

weixin_34402408發表於2016-10-19
//編碼
NSString *uniStr = [NSString stringWithUTF8String:[_barrageText.text UTF8String]];
NSData *uniData = [uniStr dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodStr = [[NSString alloc] initWithData:uniData encoding:NSUTF8StringEncoding] ;
NSLog(@"---編碼--->[%@]",goodStr);

//解碼
const char *jsonString = [goodStr UTF8String];   // goodStr 伺服器返回的 json
NSData *jsonData = [NSData dataWithBytes:jsonString length:strlen(jsonString)];
NSString *goodMsg1 = [[NSString alloc] initWithData:jsonData encoding:NSNonLossyASCIIStringEncoding];
NSLog(@"---解碼--->[%@]",goodMsg1);

2016-04-15 10:16:17.858 DFRomwe[650:153981] ---編碼--->[hello\ud83d\ude18\ud83d\ude18world\u4e16\u754chaha\ud83d\ude17]
2016-04-15 10:16:17.859 DFRomwe[650:153981] ---解碼--->[hello??world世界haha?]

相關文章