iOS 字串轉碼+解壓

來源:互聯網
上載者:User

標籤: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




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.