串去code UTF-8code、UTF

2023-09-06 15:28:29 作者:現實如此可笑

我怎么能去code的UTF-8字符串中的android?我尝试用这个命令,但其输出相同的输入:

  URLDe coder.de code(你好&放大器; // A,UTF-8);

新的String(你好&放大器; // A,UTF-8);

EntityUtils.toString(你好&放大器; // A,UTF-8);
 

解决方案

一个字符串,不需要编码。这是一个简单的统一code字符的序列。

leetcode393.UTF 8 编码验证

您需要的连接code,当你想将一个字符串转换为字节序列的。在您选择的字符集(UTF-8,cp1255等)决定的字符级>字节的映射。注意,一个字符不一定翻译成单个字节。在大多数字符集,最统一code字符被转换为至少两个字节。

一个String的编码是通过:

 字符串S1 =一些文本;
字节[]字节= s.getBytes(UTF-8); //字符集为en code成
 

您需要的德code 的,当你有一个字节序列,然后你把它们变成一个字符串。当你做,你需要指定,再次,字符集与该字节最初连接codeD(否则你会最终与乱码文本)。

解读:

 字符串s2 =新的字符串(字节,UTF-8); //字符集与字节为EN codeD
 

如果你想绝对更好的理解,一个伟大的文字是绝对最低每一个软件开发人员,积极必须了解统一code和字符集(没有借口!)

How i can decode an utf-8 string in android? I have try with this commands but output it same of input:

URLDecoder.decode("hello&//à", "UTF-8");

new String("hello&//à", "UTF-8");

EntityUtils.toString("hello&//à", "utf-8");

解决方案

A string needs no encoding. It is simply a sequence of Unicode characters.

You need to encode when you want to turn a String into a sequence of bytes. The charset the you choose (UTF-8, cp1255, etc.) determines the Character->Byte mapping. Note that a character is not necessarily translated into a single byte. In most charsets, most Unicode characters are translated to at least two bytes.

Encoding of a String is carried out by:

String s1 = "some text";
byte[] bytes = s.getBytes("UTF-8"); // Charset to encode into

You need to decode when you have a sequence of bytes and you want to turn them into a String. When you do that you need to specify, again, the charset with which the bytes were originally encoded (otherwise you'll end up with garbled text).

Decoding:

String s2 = new String(bytes, "UTF-8"); // Charset with which bytes were encoded 

If you want to understand this better, a great text is "The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)"