iOS9 字符串或url的 UTF-8 编解码

iOS9 字符串或url的 UTF-8 编解码


您需要加密的字符串

NSString *str = @"您需要加密的字符串GG...";

编码

//    被弃用的编码方法

//    NSString *encodeStr =[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

//    替代方法实现

NSString *charactersToEscape = @"?!@#$^&%*+,:;='\"`<>()[]{}/\\| ";//此处不做更改,

NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];

NSString *encodeStr = [str stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];

NSLog(@"encodeStr___%@",encodeStr);

解码

//    被弃用的解码方法

//    NSString *deCodeStr = [encodeStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString *deCodeStr = encodeStr.stringByRemovingPercentEncoding;

NSLog(@"deCodeStr__%@",deCodeStr);

你可能感兴趣的:(iOS9 字符串或url的 UTF-8 编解码)