你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS 字符串轉碼+解壓

iOS 字符串轉碼+解壓

編輯: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




  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved