gpt4 book ai didi

java - ED A0 80 ED B0 80 是有效的 UTF-8 字节序列吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:55:10 25 4
gpt4 key购买 nike

java.nio.charset.Charset.forName("utf8").decode解码

的字节序列
 ED A0 80 ED B0 80

进入 Unicode 代码点:

 U+10000

java.nio.charset.Charset.forName("utf8").decode还解码

的字节序列
 F0 90 80 80

进入 Unicode 代码点:

 U+10000

这由 code below 验证.

现在这似乎告诉我 UTF-8 编码方案会将 ED A0 80 ED B0 80F0 90 80 80 解码为相同的 unicode 代码点。

但是,如果我访问 https://www.google.com/search?query=%ED%A0%80%ED%B0%80 ,

可以看出和页面https://www.google.com/search?query=%F0%90%80%80明显不同

由于 Google 搜索也使用 UTF-8 编码方案(如果我错了请纠正我),

这表明 UTF-8 不会将 ED A0 80 ED B0 80F0 90 80 80 解码为相同的 unicode 代码点。

所以基本上我想知道,根据官方标准,UTF-8 是否应该将 ED A0 80 ED B0 80 字节序列解码为 Unicode 代码点 U+10000?

代码:

public class Test {

public static void main(String args[]) {
java.nio.ByteBuffer bb = java.nio.ByteBuffer.wrap(new byte[] { (byte) 0xED, (byte) 0xA0, (byte) 0x80, (byte) 0xED, (byte) 0xB0, (byte) 0x80 });
java.nio.CharBuffer cb = java.nio.charset.Charset.forName("utf8").decode(bb);
for (int x = 0, xx = cb.limit(); x < xx; ++x) {
System.out.println(Integer.toHexString(cb.get(x)));
}
System.out.println();
bb = java.nio.ByteBuffer.wrap(new byte[] { (byte) 0xF0, (byte) 0x90, (byte) 0x80, (byte) 0x80 });
cb = java.nio.charset.Charset.forName("utf8").decode(bb);
for (int x = 0, xx = cb.limit(); x < xx; ++x) {
System.out.println(Integer.toHexString(cb.get(x)));
}
}
}

最佳答案

ED A0 80 ED B0 80 是 UTF-16 代理项对 D800 DC00 的 UTF-8 编码。这在 UTF-8不允许 :

However, pairs of UCS-2 values between D800 and DFFF (surrogate pairs in Unicode parlance)...need special treatment: the UTF-16 transformation must be undone, yielding a UCS-4 character that is then transformed as above.

然而,这样的编码用于CESU-8和 Java 的“修改后的 UTF-8”。

Since the Google Search is using UTF-8 encoding scheme (correct me if I'm wrong) as well,

根据搜索框,Google 似乎正在使用某种编码自动检测。如果您向它传递 F0 90 80 80,这是有效的 UTF-8,它会将其解释为 UTF-8 (𐀀)。如果你传递 ED A0 80 ED B0 80,这是无效的 UTF-8,它会将其解释为 windows-1252 (í��€í°€)。

关于java - ED A0 80 ED B0 80 是有效的 UTF-8 字节序列吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8843742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com