iOS 字符串和UTF8编码转换

功能比较简单,直接上代码

NSString *demoStr = @"我是汉字";
  
//字符串转UTF8,iOS9前后的方法不一样
// = iOS9
NSString *utfStr2 = [demoStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
//如果反过来转码的话,如下
// = iOS9
NSString *demoStr2 = [utfStr2 stringByRemovingPercentEncoding];

搞定!!

你可能感兴趣的:(浅析iOS)