//身份证姓名读取高低位需要转换,unicode转汉字,每2字节代表一个汉字
NSString *name = [string substringToIndex:60];
NSString *nameStr = @"";
for (NSInteger i=0 ;i<name.length;)
{
NSString *tempstr = [name substringWithRange:NSMakeRange(i,4)];//每两字节一个汉字
if([tempstr isEqualToString:@"2000"])
{
break;
}
NSString *tempNameStr1 = [tempstr substringWithRange:NSMakeRange(2,2)];//高低位转换
NSString *tempNameStr2 = [tempstr substringWithRange:NSMakeRange(0,2)];
NSString *tempName = [@"u" stringByAppendingString:tempNameStr1];//拼接\u
tempName = [@"\\" stringByAppendingString:tempName];
tempName = [tempName stringByAppendingString:tempNameStr2];
nameStr = [nameStr stringByAppendingString:tempName];
i+=4;
}
nameStr = [weakSelf replaceUnicode:nameStr];
- (NSString*) replaceUnicode:(NSString*)TransformUnicodeString
{
NSString*tepStr1 = [TransformUnicodeString stringByReplacingOccurrencesOfString:@"\\u"withString:@"\\U"];
NSString*tepStr2 = [tepStr1 stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""];
NSString*tepStr3 = [[@"\""stringByAppendingString:tepStr2]stringByAppendingString:@"\""];
NSData*tepData = [tepStr3 dataUsingEncoding:NSUTF8StringEncoding];
NSString*axiba = [NSPropertyListSerialization propertyListWithData:tepData options:NSPropertyListMutableContainers format:NULL error:NULL];
return [axiba stringByReplacingOccurrencesOfString:@"\\r\\n"withString:@"\n"];
}
汉字转gbk
NSStringEncodin gbkEncoding=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSData*aData = [“要转的汉字” dataUsingEncoding: gbkEncoding];
NSString *result = [NSStringTool convertDataToHexStr:aData];