- Unicode转化为汉字
+ (NSString *)replaceUnicode:(NSString *)unicodeStr {
NSString *tempStr1 = [unicodeStrstringByReplacingOccurrencesOfString:@"\\u"withString:@"\\U"];
NSString *tempStr2 = [tempStr1stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""];
NSString *tempStr3 = [[@"\""stringByAppendingString:tempStr2]stringByAppendingString:@"\""];
NSData *tempData = [tempStr3dataUsingEncoding:NSUTF8StringEncoding];
NSString* returnStr = [NSPropertyListSerializationpropertyListFromData:tempData
mutabilityOption:NSPropertyListImmutable
format:NULL
errorDescription:NULL];
return [returnStrstringByReplacingOccurrencesOfString:@"\\r\\n"withString:@"\n"];
}
- 汉字与utf8相互转化
NSString* strA = [@"%E4%B8%AD%E5%9B%BD"stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *strB = [@"中国"stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- NSString 转化为utf8
NSString *strings = [NSStringstringWithFormat:@"abc"];
NSLog(@"strings : %@",strings);
CF_EXPORT
CFStringRef CFURLCreateStringByAddingPercentEscapes(CFAllocatorRef allocator,CFStringReforiginalString,CFStringRef charactersToLeaved, CFStringReflegalURLCharactersToBeEscaped,CFStringEncoding encoding);
NSString *encodedValue = (__bridge NSString*)CFURLCreateStringByAddingPercentEscapes(nil, (__bridgeCFStringRef)strings,nil, (CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8);
- iso8859-1 到 unicode编码转换
+ (NSString *)changeISO88591StringToUnicodeString:(NSString *)iso88591String
{
NSMutableString *srcString = [[[NSMutableString alloc]initWithString:iso88591String]autorelease];
[srcString replaceOccurrencesOfString:@"&" withString:@"&" options:NSLiteralSearchrange:NSMakeRange(0, [srcString length])];
[srcString replaceOccurrencesOfString:@"&#x" withString:@"" options:NSLiteralSearchrange:NSMakeRange(0, [srcString length])];
NSMutableString *desString = [[[NSMutableString alloc]init] autorelease];
NSArray *arr = [srcString componentsSeparatedByString:@";"];
for(int i=0;i<[arr count]-1;i++){
NSString *v = [arr objectAtIndex:i];
char *c = malloc(3);
int value = [StringUtil changeHexStringToDecimal:v];
c[1] = value &0x00FF;
c[0] = value >>8 &0x00FF;
c[2] = '\0';
[desString appendString:[NSString stringWithCString:c encoding:NSUnicodeStringEncoding]];
free(c);
}
return desString;
}
//NSData 转 NSString
NSData *data;
NSString*str = [[NSStringalloc] initWithData:data encoding:NSUTF8StringEncoding];
//NSString 转 NSData
NSString *string;
NSData*data = [string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
//char 转 NSString
char *str1;
NSString*str = [NSString stringWithCString:str1 encoding:NSUTF8StringEncoding];
//NSString 转 char
NSString *str;
char *str1 = [str UTF8String];
//自转换
NSString*string;
string = [string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//也可以修改NSString类
//NSString+URLEncoding.h
#import<Foundation/Foundation.h>
@interface NSString (OAURLEncodingAdditions)
- (NSString *)URLEncodedString;
- (NSString *)URLDecodedString;
@end
//NSString+URLEncoding.m
#import"NSString+URLEncoding.h"
@implementation NSString (OAURLEncodingAdditions)
- (NSString *)URLEncodedString
{
NSString*result = (NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)self,
NULL,
CFSTR("!*'();:@&=+$,/?%#[]"),
kCFStringEncodingUTF8);
[result autorelease];
return result;
}
//解决url乱码
- (NSString*)URLDecodedString
{
NSString*result = (NSString*)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,
(CFStringRef)self,
CFSTR(""),
kCFStringEncodingUTF8);
[result autorelease];
return result;
}
@end
转载地址:http://www.th7.cn/Program/IOS/201208/87071.shtml
http://blog.csdn.net/jidiao/article/details/8456370