简体   繁体   English

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

[英]Convert Unicode to UTF-8 byte[] and save into string (Java)

I need to convert the character unicode to a byte[] representation and save into Srting, for example 我需要将字符unicode转换为byte []表示形式并保存到Srting中,例如

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

I dont have idea how can i do it this.. Anyone has idea?Thanks 我不知道该怎么办。任何人都知道吗?

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));

So one first coposes the Unicode code points into a java String. 因此,首先要将Unicode代码点合并到一个Java字符串中。 Java Strings hold Unicode. Java字符串包含Unicode。 When one wants bytes, say in UTF-8 - a Unicode representation -, then one has to indicate the CharSet in which the bytes will be. 当有人想要字节时,例如用UTF-8(一种Unicode表示形式)说,那么人们就必须指明字节所在的CharSet。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM