博客> iOS中汉字的处理
iOS中汉字的处理
2019-12-10 15:00 评论:0 阅读:833 gedefu
iOS字符串 汉字 转换

//将汉字字符串转换成UTF8字符串 +(NSString )chineseToUTf8Str:(NSString)chineseStr{ NSStringEncoding encodingUTF8 = NSUTF8StringEncoding; NSData responseData2 =[chineseStr dataUsingEncoding:encodingUTF8 ]; NSString string=[sendSocket NSDataToByteTohex:responseData2]; return string; }

//将十六进制字符串转换成汉字 +(NSString)changeLanguage:(NSString)chinese{ NSString strResult; NSLog(@"chinese:%@",chinese); if (chinese.length%2==0) { //第二次转换 NSData newData = [sendSocket hexToByteToNSData:chinese]; unsigned long encode = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000); strResult = [[NSString alloc] initWithData:newData encoding:encode]; NSLog(@"strResult:%@",strResult); }else{ NSString *strResult = @"已假定是汉字的转换,所传字符串的长度必须是4的倍数!"; NSLog(@"%@",strResult); return NULL; } return strResult; }

/////////////GBK,汉字,GB2312,ASCII码,UTF8,UTF16 //UTF8字符串转换成汉字 +(NSString)changeLanguageUTF8:(NSString)chinese{ NSString strResult; NSData data=[sendSocket hexToByteToNSData:chinese]; strResult=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; return strResult; }

收藏
0
sina weixin mail 回到顶部