iOS 字符串转码+解压

最近遇到这样的一个问题,从服务器上返回的字符串是经过压缩的。下面记录下解决方法


//
//  ViewController.m
//  字符串解压缩
//
//  Created by 杜甲 on 14-5-8.
//  Copyright (c) 2014年 杜甲. All rights reserved.
//

#import "ViewController.h"
#import "LFCGzipUtillity.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    //测试字符串
    NSString* data1r = @"%1F%C2%8B%08%00%00%00%00%00%00%00%05%C3%81%C3%81%11%00+%08%04%C2%B1j%C2%B6%01F%10%C2%9E%C2%A0g%C3%BF%25%C2%99%60%0F%05%C3%A3%C3%A4%C2%A0%C2%A4%1F%C3%A5%C3%88%C3%A9Km%C2%B4%C3%A9C%07ZL2%C3%B6%01%C3%84%C3%B2%C2%A5+0%00%00%00:222";//[request responseString];
    NSLog(@"%@",data1r);
    //第一次解码
    NSString* decode = [ self StringDecode:data1r];
    //转换成NSData
    NSData* data = [decode dataUsingEncoding:NSISOLatin1StringEncoding];
    //解压缩
    NSData* data13 = [LFCGzipUtillity uncompressZippedData:data];
    //
    NSString* str = [[NSString alloc] initWithData:data13 encoding:NSUTF8StringEncoding];
    //解码
    NSString* decode1 = [self StringDecode:str];
    NSLog(@"decode1 = %@",decode1);
}
- (NSString *)StringDecode:(NSString*)str
{
    return [[str stringByReplacingOccurrencesOfString:@"+" withString:@" "] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

输出:

decode1 = /测试字符串


代码下载:http://download.csdn.net/detail/qqmcy/7314665




你可能感兴趣的:(ios,字符串,编码,解压)