Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Unicode转换为UTF-8字节[]并保存为字符串(Java)_Java_Unicode_Utf 8 - Fatal编程技术网

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

将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

例如,我需要将字符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表示法)中,那么他必须指出字节将在其中的字符集。

用这个代码我得到了[-16,-97,-104,-127]结果..不是我想要的..否?-16=F0 hex,-97=256-97=159=160-1=hex A0-1=9F。所以没关系。