将Unicode转换为UTF-8字节[]并保存为字符串(Java)
例如,我需要将字符unicode转换为byte[]表示形式并保存为Srting将Unicode转换为UTF-8字节[]并保存为字符串(Java),java,unicode,utf-8,Java,Unicode,Utf 8,例如,我需要将字符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
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表示法)中,那么他必须指出字节将在其中的字符集。用这个代码我得到了[-16,-97,-104,-127]结果..不是我想要的..否?-16=F0 hex,-97=256-97=159=160-1=hex A0-1=9F。所以没关系。