美文网首页IOS
字符串转UTF-8遇到的坑

字符串转UTF-8遇到的坑

作者: Teehom | 来源:发表于2015-10-17 23:59 被阅读239次

    在实际的开发中,遇到了些问题,就是一个带中文的字符串,怎么转UTF-8的问题。

    这个字符串是“788中国%”,我想到没想,就这样做了

    NSString*str3 = [str1 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    然后转出来是nil,这不是我们想要的。发现只要有%包含,都会返回nil。

    尝试了很多方法,最后尝试使用了如下方法。先转为utf-8的字符类型,再转成字符串,问题就解决了

    const char *wNameChar = [self.wifiName cStringUsingEncoding:NSUTF8StringEncoding];

    const char *wPassdChar = [self.wifiPass cStringUsingEncoding:NSUTF8StringEncoding];

    NSString *currentName = [[NSString alloc] initWithUTF8String:wNameChar];

    NSString *currentPassword = [[NSString alloc] initWithUTF8String:wPassdChar];

    相关文章

      网友评论

        本文标题:字符串转UTF-8遇到的坑

        本文链接:https://www.haomeiwen.com/subject/frmvqttx.html