Java 包含到ASCII的汉字的字符串

Java 包含到ASCII的汉字的字符串,java,eclipse,encoding,ascii,Java,Eclipse,Encoding,Ascii,这是我的代码,两种方法都提供相同的输出 String description = "test string with 音樂"; byte[] b = description.getBytes("US-ASCII"); //first way char[] result = new char[b.length]; for ( int i = 0; i < b.length; i++ ) { result[i] = (char)b[i]; } System.out.pr

这是我的代码,两种方法都提供相同的输出

String description = "test string with 音樂";
byte[] b = description.getBytes("US-ASCII");

//first way
char[] result = new char[b.length];       
for ( int i = 0; i < b.length; i++ ) {
    result[i] = (char)b[i];
}
System.out.println(new String(result)); //output - test string with ??

//second way
System.out.println(new String(b, "UTF-8")); //output - test string with ??
String description=“使用音樂";
字节[]b=description.getBytes(“US-ASCII”);
//第一条路
char[]结果=新字符[b.长度];
for(int i=0;i
我正在使用Eclipse并在运行配置下将控制台输出编码更改为US-ASCII

是否可以将其作为US-ASCII编码字符串获取


谢谢,adv!!!

无法将汉字转换为US-ASCII,因为它们不包含在此字符集中


US-ASCII只知道128个不同的字符,其中一些甚至是非打印控制字符。

无法将中文字符转换为US-ASCII,因为这些字符不包含在此字符集中


US-ASCII只知道128个不同的字符,其中一些甚至是非打印控制字符。

无法将其转换为US-ASCII,但是

如果您只需要Unicode转义字符串,那么可以使用apache common lang实用程序

import org.apache.commons.lang.StringEscapeUtils;

...
StringEscapeUtils.unescapeJava("test string with \u97F3\u6A02"); 
 //gives result : test string with 音樂
StringEscapeUtils.escapeJava("test string with 音樂"); 
 //gives result : test string with \u97F3\u6A02

无法将其转换为US-ASCII,但是

如果您只需要Unicode转义字符串,那么可以使用apache common lang实用程序

import org.apache.commons.lang.StringEscapeUtils;

...
StringEscapeUtils.unescapeJava("test string with \u97F3\u6A02"); 
 //gives result : test string with 音樂
StringEscapeUtils.escapeJava("test string with 音樂"); 
 //gives result : test string with \u97F3\u6A02

那么“测试字符串”的输出是什么音樂“?你想达到什么目的?你知道ASCII根本没有这些字符,对吧?@JonSkeet我想把它转换成ASCII表示字符串。有可能吗?事实上,我是unicode的新手。如果你想要汉字,答案是“不”。你应该仔细阅读ASCII:那么你想要什么输出?”带有音樂“?你想达到什么目的?你知道ASCII根本没有这些字符,对吗?@JonSkeet我正试图将其转换为ASCII表示字符串。有可能吗?事实上,我是unicode新手。如果你想要汉字,答案是“没有”“。您应该在ASCII上阅读:因此字符串是否包含中文或法文字符是不可能的?否。Jon Skeet提供的链接显示了所有可能的字符。因此字符串是否包含中文或法文字符是不可能的?否。Jon Skeet提供的链接显示了所有可能的字符。