美文网首页
iOS UTF-8编码/解码 UrlEncode编码/解码

iOS UTF-8编码/解码 UrlEncode编码/解码

作者: Rumbles | 来源:发表于2019-03-11 11:22 被阅读1次
    UTF-8编码/解码
    编码
    ////>表达我今天的心情
        NSString *urlCode = @"https://www.baidu.com?a=n去你妈";
        
        NSString *hString = [urlCode stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        
        NSString *str5 = [hString stringByRemovingPercentEncoding];
        
        NSLog(@"UTF-8编码:%@  解码:%@",hString,str5);
    编码:  https://www.baidu.com?a=n%E5%8E%BB%E4%BD%A0%E5%A6%88  
    解码:https://www.baidu.com?a=n去你妈
    
    UrlEncode编码/解码
    ///> URLEncode
    - (NSString *)URLEncodedString:(NSString*)urlStr {
        NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)urlStr,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8));
        return encodedString;
    }
    
    
    ///> URLDecode
    -(NSString *)URLDecodedString:(NSString*)urlStr {
        NSString *decodedString  = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,(__bridge CFStringRef)urlStr,CFSTR(""),CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
        return decodedString;
    }
    
    编码:https%3A%2F%2Fwww.baidu.com%3Fa%3Dn%E5%8E%BB%E4%BD%A0%E5%A6%88  
    解码:https://www.baidu.com?a=n去你妈
    

    ///> 区别

    UrlEncode编码/解码 会对 "!*'();:@&=+$,/?%#[] 进行编码
    
    sname=ç²¾é��&pname=ç�´æ�­ä¸­å�½-å
    ¨é�¨ç�´æ�­+-+45??
    
    
    怎么出现的呢?
    http://cbox.cntv.cn/special/cbox/liveChinaList/index.html?sname=精选&pname=直播中国-全部直播 - 45
    UrlEncode编码
    UTF-8解码
    就回出现乱码
    
    

    相关文章

      网友评论

          本文标题:iOS UTF-8编码/解码 UrlEncode编码/解码

          本文链接:https://www.haomeiwen.com/subject/gfevpqtx.html