escape解密

+ (NSString *)replaceUnicodeTest:(NSString *)test

{// test为需要解码的字符串

    NSString *body = test;

    NSMutableString *mutableStr = [[NSMutableString alloc] initWithCapacity:1];

    NSScanner*scanner=[NSScanner scannerWithString:body];

    [scanner setCaseSensitive:YES]; // yes 区分大小写

    NSString *keyString01 = @"%";

    

    int lastPos = 0int pos = 0;

    while (lastPos < body.length) {

        @autoreleasepool{

            pos = [self indexOf:body andPre:keyString01 andStartLocation:lastPos];

            if (pos == lastPos) {

                // 转为unicode 编码 再解码

                if ([body characterAtIndex:(pos + 1)] == 'u') {

                    NSRange range = NSMakeRange(pos, 6);

                    NSString *tempBody =[body substringWithRange:range];

                    NSString *temp01 = [tempBody stringByReplacingOccurrencesOfString:@"%" withString:@"\\"];

                    NSString *temp02 = [self replaceUnicode:temp01]; // 转为中文

                    //                NSLog(@"--%@",temp02);

                    [mutableStr appendString:temp02];

                    lastPos = pos + 6;

                } else {

                    NSRange range = NSMakeRange(pos, 3);

                    NSString *tempBody =[body substringWithRange:range];

                    NSString *temp01 = [tempBody stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

//                    NSLog(@"--%@",temp01);

                    [mutableStr appendString:temp01];

                    lastPos = pos + 3;

                }

            }else if (pos == -1) {

                NSString *tempBody =[body substringFromIndex:lastPos];

                [mutableStr appendFormat:@"%@",tempBody];

                lastPos = (int)body.length;

            }else {

                NSRange range = NSMakeRange(lastPos, pos-lastPos);

                NSString *tempBody =[body substringWithRange:range];

                [mutableStr appendString:tempBody];

                lastPos = pos;

            }

        }

    }

    

    return mutableStr;

}

// 转为中文

+ (NSString *)replaceUnicode:(NSString *)msg

{

    NSString *tempStr1 = [msg stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];

    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\""];

    NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];

    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF16StringEncoding];

    NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData

                                                           mutabilityOption:NSPropertyListImmutable

                                                                     format:NULL

                                                           errorDescription:NULL];

    return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"];

}

// 查找有咩有百分号 并返回ta的位置

+ (int)indexOf:(NSString *)allStr andPre:(NSString *)pre andStartLocation:(int)StartLocation

{

    NSString *body = [allStr substringFromIndex:StartLocation];

    NSScanner*scanner=[NSScanner scannerWithString:body];

    NSString *keyString01 = pre;

    [scanner setCaseSensitive:YES]; // yes 区分大小写

    

    BOOL b = NO;

    int returnInt = 0;

    while (![scanner isAtEnd]) {

        b = [scanner scanString:keyString01 intoString:NULL];

        if(b) {

            returnInt = StartLocation + (int)scanner.scanLocation - 1;

            break;

        }

        

        scanner.scanLocation++;

    }

    

    if (!b) {

        returnInt = -1;

    }

    

    return returnInt;

    

    

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值