// // Utility.m // // // Created by apple on 10-7-23. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import "Utility.h" //使用头文件 #import "base64.h" @implementation Utility + (NSString*)encodeBase64:(NSString*)input { size_t inputDataSize =[input lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; Byte inputData[inputDataSize];//prepare a Byte[] [[input dataUsingEncoding:NSUTF8StringEncoding] getBytes:inputData];//get the pointer of the data size_t outputDataSize = EstimateBas64EncodedDataSize(inputDataSize);//calculate the encode data size Byte outputData[outputDataSize];//prepare a Byte[] for the encode data Base64EncodeData(inputData, inputDataSize, outputData, &outputDataSize,YES);//encode the data NSData * theData = [[NSData alloc] initWithBytes:outputData length:outputDataSize]; NSString *result = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]; return result; } + (NSString*)decodeBase64:(NSString*)input { size_t inputDataSize =[input lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; Byte inputData[inputDataSize];//prepare a Byte[] [[input dataUsingEncoding:NSUTF8StringEncoding] getBytes:inputData];//get the pointer of the data size_t outputDataSize = EstimateBas64DecodedDataSize(inputDataSize);//calculate the decode data size Byte outputData[outputDataSize];//prepare a Byte[] for the decode data Base64DecodeData(inputData, inputDataSize, outputData, &outputDataSize);//decode the data NSData * theData = [[NSData alloc] initWithBytes:outputData length:outputDataSize]; NSString *result = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]; return result; } + (NSString*)URLEncodedString:(NSString*)input { NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)input, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8); [result autorelease]; return result; } + (NSString*)URLDecodedString:(NSString*)input { NSString *result = (NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)input, CFSTR(""), kCFStringEncodingUTF8); [result autorelease]; return result; } @end