如何将NSString转换为UTF8Encode? p>
我的NSLog打印与输入字符串相同的结果
NSString* username =@"MYUSERNAME";
username=[username stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString* Password =@"MYUSERNAMEPASS";
Password=[Password stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"username=%@",username);
NSLog(@"Password=%@",Password);
答案 0 :(得分:3)
您需要使用stringByAddingPercentEscapesUsingEncoding
代替stringByReplacingPercentEscapesUsingEncoding
。
NSString *encodeUserName = [username stringByAddingPercentEscapesUsingEncoding:NSUTF32StringEncoding];
此外,stringByAddingPercentEscapesUsingEncoding
也已弃用,因此请使用stringByAddingPercentEncodingWithAllowedCharacters
这样的
NSString *encodeUserName = [username stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];