java – 字符串解码utf-8
发布时间:2020-12-14 05:09:13 所属栏目:Java 来源:网络整理
导读:我如何解码一个utf-8字符串在 android?我尝试使用这个命令,但输出相同的输入: URLDecoder.decode("hello//à","UTF-8");new String("hello//à","UTF-8");EntityUtils.toString("hello//à","utf-8"); 解决方法 字符串不需要编码.这只是一个Unicode字符序
我如何解码一个utf-8字符串在
android?我尝试使用这个命令,但输出相同的输入:
URLDecoder.decode("hello&//à","UTF-8"); new String("hello&//à","UTF-8"); EntityUtils.toString("hello&//à","utf-8"); 解决方法
字符串不需要编码.这只是一个Unicode字符序列.
当您想将String转换成一个字节序列时,需要进行编码.您选择的字符集(UTF-8,cp1255等)确定字符>字节映射.请注意,字符不一定被翻译成单个字节.在大多数字符集中,大多数Unicode字符被转换为至少两个字节. 字符串的编码是通过以下方式执行的: String s1 = "some text"; byte[] bytes = s1.getBytes("UTF-8"); // Charset to encode into 当您有字节序列时,您需要进行解码,并将其转换为字符串.当你需要再次指定该字符串最初被编码的字符集(否则你最终会使用garblеdtеxt). 解码: String s2 = new String(bytes,"UTF-8"); // Charset with which bytes were encoded 如果你想更好地理解这个,一个伟大的文本是“The Absolute Minimum Every Software Developer Absolutely,Positively Must Know About Unicode and Character Sets (No Excuses!)” (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |