iOS 基本编码格式转化

iOS中编码转化

1.UTF-8转化

    NSString *data = @"你好,北京!";

    //转换成UTF-8

    NSString *dataUTF8 = [datastringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSLog(@"%@",dataUTF8);

    //UTF-8GBK,将UTF8代码替换,官方解释如下。

//Replaces all percent escapes with the matching characters as determined by the given encoding.  Returns nil if the transformation is not possible (i.e. the percent escapes give a byte sequence not legal in the given encoding).  See CFURLCreateStringByReplacingPercentEscapes in CFURL.h for more complex transformations

    NSString *dataGBK = [dataUTF8stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSLog(@"%@",dataGBK);


在Xcode4.2中执行结果如下:


将上述方法封装,如下:

//UnicodeUTF-8

+ (NSString *)encodeToPercentEscapeString: (NSString *) input  

{  

    // Encode all the reserved characters, per RFC 3986  

    // (<http://www.ietf.org/rfc/rfc3986.txt>)  

    NSString *outputStr = (NSString *)   

    CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,  

                                            (CFStringRef)input,  

                                            NULL,  

                                            (CFStringRef)@"!*'();:@&=+$,/?%#[]",  

                                            kCFStringEncodingUTF8);  

    return outputStr;  

}  


+ (NSString *)decodeFromPercentEscapeString: (NSString *) input  

{  

    NSMutableString *outputStr = [NSMutableStringstringWithString:input];  

    [outputStr replaceOccurrencesOfString:@"+"  

                               withString:@" "  

                                  options:NSLiteralSearch  

                                    range:NSMakeRange(0, [outputStrlength])];  

    

    return [outputStrstringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  

}

2.UTF-8和Unicode转化

//UnicodeUTF-8

+ (NSString*) replaceUnicode:(NSString*)aUnicodeString

{

    NSString *tempStr1 = [aUnicodeStringstringByReplacingOccurrencesOfString:@"\\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"]; 

}



+(NSString *) utf8ToUnicode:(NSString *)string

{

    NSUInteger length = [string length];

    NSMutableString *s = [NSMutableStringstringWithCapacity:0];

    for (int i = 0;i < length; i++) 

    {

        unichar _char = [string characterAtIndex:i];

        //判断是否为英文和数字

        if (_char <= '9' && _char >='0'

        {

            [s appendFormat:@"%@",[stringsubstringWithRange:NSMakeRange(i,1)]];

        }

        else if(_char >='a' && _char <= 'z')

        {

            [s appendFormat:@"%@",[stringsubstringWithRange:NSMakeRange(i,1)]];

            

        }

        else if(_char >='A' && _char <= 'Z')

        {

            [s appendFormat:@"%@",[stringsubstringWithRange:NSMakeRange(i,1)]];

            

        }

        else

        {

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

        }

    }

    return s;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值