gpt4 book ai didi

java - 将 Unicode 转换为 UTF-8 byte[] 并保存为字符串 (Java)

转载 作者:太空宇宙 更新时间:2023-11-04 06:31:09 25 4
gpt4 key购买 nike

例如,我需要将字符 unicode 转换为 byte[] 表示形式并保存到 Srting

U+1F601 -> \xF0\x9F\x98\x81

我不知道我该怎么做..有人有想法吗?谢谢

最佳答案

int[] codepoints = { 0x1F601 }; // U+1F601
String s = new String(codepoints, 0, codepoints.length);
byte[] bytes = s.getBytes(StandardCharsets.UTF_8); // As UTF-8 (Unicode) bytes
System.out.println(Arrays.toString(bytes));

因此,首先将 Unicode 代码点组合成一个 java 字符串。 Java 字符串保存 Unicode。当需要字节时,例如使用 UTF-8(一种 Unicode 表示形式),则必须指示字节所在的 CharSet。

关于java - 将 Unicode 转换为 UTF-8 byte[] 并保存为字符串 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26099939/

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