最近遇到這樣的一個問題,從服務器上返回的字符串是經過壓縮的。下面記錄下解決方法
// // 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