java输出全部的unicode码
时间: 2023-05-15 19:05:13 浏览: 320
可以使用以下代码输出全部的unicode码:
```java
public class UnicodePrinter {
public static void main(String[] args) {
for (int i = 0; i <= 0x10FFFF; i++) {
System.out.print((char) i);
}
}
}
```
这段代码会输出所有的unicode字符,包括控制字符和不可见字符。
相关问题
java汉字转unicode编码并输出为short数组
可以在汉字转Unicode编码的基础上,再将每个Unicode编码值转成short类型,最后将所有short值存储到short数组中。示例代码如下:
```
public static short[] chineseToShortArray(String str) {
int length = str.length();
short[] result = new short[length];
for (int i = 0; i < length; i++) {
char c = str.charAt(i);
if (c >= 0 && c <= 127) {
result[i] = (short) c;
} else {
result[i] = (short) Integer.parseInt(Integer.toHexString(c), 16);
}
}
return result;
}
```
使用方式:
```
String str = "你好";
short[] shorts = chineseToShortArray(str);
for (short s : shorts) {
System.out.print(s + " ");
}
```
输出结果:
```
20320 22909
```
其中,20320和22909分别是汉字“你”和“好”的Unicode编码值,将它们转成short类型后就是20320和22909。
java汉字转unicode编码
可以使用Java内置的Character类中的静态方法来将汉字转成Unicode编码,示例代码如下:
```
public static String chineseToUnicode(String str) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c >= 0 && c <= 127) {
result.append(c);
} else {
result.append("\\u").append(Integer.toHexString(c));
}
}
return result.toString();
}
```
使用方式:
```
String str = "你好";
String unicode = chineseToUnicode(str);
System.out.println(unicode);
```
输出结果:
```
\u4f60\u597d
```
其中,`\u`表示后面跟随的是16进制的Unicode编码值。所以,`\u4f60`就是汉字“你”的Unicode编码值,`\u597d`就是汉字“好”的Unicode编码值。