美文网首页
iOS escape编解码方法

iOS escape编解码方法

作者: 路小白同学 | 来源:发表于2019-08-27 10:39 被阅读0次

escape是JavaScript escape() 函数,可以对字符串进行编解码,iOS中并没有这中编解码方法,但有时候我们与后台交互,还得用到这种编解码格式,查了一下,终于找到了一份用C写的escape编解码方法,做下记录。
esp:编码方法
unesp:解码方法

NSString * tohex(int tmpid)
{
    NSString *nLetterValue;
    NSString *str =@"";
    long long int ttmpig;
    for (int i = 0; i<9; i++) {
        ttmpig=tmpid%16;
        tmpid=tmpid/16;
        switch (ttmpig)
        {
            case 10:
                nLetterValue =@"A";break;
            case 11:
                nLetterValue =@"B";break;
            case 12:
                nLetterValue =@"C";break;
            case 13:
                nLetterValue =@"D";break;
            case 14:
                nLetterValue =@"E";break;
            case 15:
                nLetterValue =@"F";break;
            default:nLetterValue=[[NSString alloc]initWithFormat:@"%lli",ttmpig];
                
        }
        str = [nLetterValue stringByAppendingString:str];
        if (tmpid == 0) {
            break;
        }
        
    }
    return str;
}


NSString * esp(NSString * src){
    int i;
    
    
    NSString* tmp = @"";
    
    
    for (i=0; i<[src length]; i++) {
        unichar c  = [src characterAtIndex:(NSUInteger)i];
        
        
        if(isdigit(c)||isupper(c)|islower(c)){
            tmp = [NSString stringWithFormat:@"%@%c",tmp,c];
        }else if((int)c <256){
            tmp = [NSString stringWithFormat:@"%@%@",tmp,@"%"];
            if((int)c <16){
                tmp =[NSString stringWithFormat:@"%@%@",tmp,@"0"];
            }
            tmp = [NSString stringWithFormat:@"%@%@",tmp,tohex((int)c)];
            
        }else{
            tmp = [NSString stringWithFormat:@"%@%@",tmp,@"%u"];
            tmp = [NSString stringWithFormat:@"%@%@",tmp,tohex(c)];
            
        }
        
        
    }
    
    
    return tmp;
}
Byte getInt(char c){
    if(c>='0'&&c<='9'){
        return c-'0';
    }else if((c>='a'&&c<='f')){
        return 10+(c-'a');
    }else if((c>='A'&&c<='F')){
        return 10+(c-'A');
    }
    return c;
}
int  getIntStr(NSString *src,int len){
    if(len==2){
        Byte c1 = getInt([src characterAtIndex:(NSUInteger)0]);
        Byte c2 = getInt([src characterAtIndex:(NSUInteger)1]);
        return ((c1&0x0f)<<4)|(c2&0x0f);
    }else{
        
        Byte c1 = getInt([src characterAtIndex:(NSUInteger)0]);
        
        Byte c2 = getInt([src characterAtIndex:(NSUInteger)1]);
        Byte c3 = getInt([src characterAtIndex:(NSUInteger)2]);
        Byte c4 = getInt([src characterAtIndex:(NSUInteger)3]);
        return( ((c1&0x0f)<<12)
               |((c2&0x0f)<<8)
               |((c3&0x0f)<<4)
               |(c4&0x0f));
    }
    
}
NSString* unesp(NSString* src){
    int lastPos = 0;
    int pos=0;
    unichar ch;
    NSString * tmp = @"";
    while(lastPos<src.length){
        NSRange range;
        
        range = [src rangeOfString:@"%" options:NSLiteralSearch range:NSMakeRange(lastPos, src.length-lastPos)];
        if (range.location != NSNotFound) {
            pos = (int)range.location;
        }else{
            pos = -1;
        }
        
        if(pos == lastPos){
            
            if([src characterAtIndex:(NSUInteger)(pos+1)]=='u'){
                NSString* ts = [src substringWithRange:NSMakeRange(pos+2,4)];
                
                int d = getIntStr(ts,4);
                ch = (unichar)d;
                NSLog(@"%@%C",tohex(d),ch);
                tmp = [tmp stringByAppendingString:[NSString stringWithFormat:@"%C",ch]];
                
                lastPos = pos+6;
                
            }else{
                NSString* ts = [src substringWithRange:NSMakeRange(pos+1,2)];
                int d = getIntStr(ts,2);
                ch = (unichar)d;
                tmp = [tmp stringByAppendingString:[NSString stringWithFormat:@"%C",ch]];
                lastPos = pos+3;
            }
            
        }else{
            if(pos ==-1){
                NSString* ts = [src substringWithRange:NSMakeRange(lastPos,src.length-lastPos)];
                
                tmp = [tmp stringByAppendingString:[NSString stringWithFormat:@"%@",ts]];
                lastPos = (int)src.length;
            }else{
                NSString* ts = [src substringWithRange:NSMakeRange(lastPos,pos-lastPos)];
                
                tmp = [tmp stringByAppendingString:[NSString stringWithFormat:@"%@",ts]];
                lastPos  = pos;
            }
        }
    }
    
    return tmp;
}


相关文章

  • iOS escape编解码方法

    escape是JavaScript escape() 函数,可以对字符串进行编解码,iOS中并没有这中编解码方法,...

  • 功能-使用Base64编解码

    iOS之后,NSData新增一个类别用于编解码NSData。 编解码NSString: 以上的方法是通用方法,不过...

  • iOS escape编码方法

    iOS中网络传输不可避免需要上传,后端需要中文进行escape编码处理后上传。用NSString中的方法: 并不能...

  • escape encodeURI encodeURIString

    escape 方法 encodeURI 方法 encodeURIComponent 方法

  • 关于编码

    一、escape,encodeURI,encodeURIComponent 1. escape 方法描述 对 St...

  • H264 编解码

    H264编解码(一) —— 基本概览H264编解码(二) —— ios中的H264硬编解码的实现H264编解码(三...

  • 多媒体音频播放简单介绍

    概述 基础知识-音频编解码,音频格式,音频会话(session) ios软硬件音频编解码器 音频会话(Audio ...

  • iOS下 AAC 音频编码

    前言 iOS下Apple为我们提供了非常方便的音频编解码工具AudioToolbox。该工具中包含了常见的编解码库...

  • 如何用终端命令&iOS代码base64编解码?

    『导语』 在iOS开发中base64如何用xcode进行编解码?如何用终端进行编解码?我将用以下例子说明:(xco...

  • iOS微信唤起,浏览器唤起app中遇到的问题

    当连接中含有的值 有汉字时,需要对汉字进行编码。如果h5页面使用了js中的escape方法进行了编码。ios可以使...

网友评论

      本文标题:iOS escape编解码方法

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