Java 如何以我输入的ASCII码显示所需值?

Java 如何以我输入的ASCII码显示所需值?,java,ascii,Java,Ascii,我需要Java代码。请帮帮我。示例:当我以ASCII输入数字时 0 the output will be nul 1 for soh 2 for stx until it reaches the max number of ASCII. 考虑一下这段代码。它输出一个ASCII码。我怎样才能逆转它 String test = "ABCD"; for ( int i = 0; i < test.length(); ++i ) { char c = test.charAt( i );

我需要Java代码。请帮帮我。示例:当我以ASCII输入数字时

0 the output will be nul
1 for soh
2 for stx until it reaches the max number of ASCII.
考虑一下这段代码。它输出一个ASCII码。我怎样才能逆转它

String test = "ABCD";
for ( int i = 0; i < test.length(); ++i ) {
    char c = test.charAt( i );
    int j = (int) c;
    System.out.println(j);
}
String test=“ABCD”;
对于(int i=0;i
只需将整数值强制转换为字符:

int value = (int) 'a';
System.out.println((char) value);  // prints a

如果需要对“0”以下的ASCII值进行文字输出,则需要从整数值(ASCII数)到文字的映射,如下所示:

String[] literals0to32 = {"NUL", "SOH", "STX", /* to be continued */ };

private static String toLiteral(int value) {

   if (value < 0 || value > 255)
      throw new IKnowThatIHaveToValidateParametersException();

   if (value < 32) 
     return literals0To32[value];
   else
     return (char) value;
}
String[]literals0to32={“NUL”、“SOH”、“STX”、/*待续*/};
私有静态字符串到文件(int值){
如果(值<0 | |值>255)
抛出新的我知道我必须验证参数sexception();
如果(值<32)
返回literals0To32[值];
其他的
返回(char)值;
}
导入java.io.*;
导入java.lang.*;
公共类CharToASCII{
公共静态void main(字符串args[])引发IOException{
BufferedReader buff=新的BufferedReader(新的InputStreamReader(System.in));
System.out.println(“输入字符:”);
字符串str=buff.readLine();
对于(int i=0;i
您可以打印相应的,例如␀ (
nul
)。

试试这个:

public static void main(String[] args) {
    String a = "a";
    char b = a.charAt(0);
    int c = b;
    System.out.println(c);
}

谢谢,但是当我的教授输入数字0时,它将打印nul。不,在这种情况下,它将打印未定义的内容。编辑我的答案以获得ASCII的快速解决方案(片段),你可以将所有代码放在公共类中重新编辑。请。@jhoanne-当然。。。没有你已经接受了另一个答案,我已经为一些闻起来像家庭作业的事情提供了太多的帮助。我不是在创建复制粘贴解决方案;(谢谢你给我你的信息。这对我真的很有帮助。但这不是我的家庭作业。再次抱歉。@jhoanne你需要进行适当的导入。你可以反转此代码的输出吗?而不是输入字符:我可以使用输入ascii码吗。例如。(输入ascii代码:0字符中的0为NUL@jhoanne因此,您需要将
int
转换为
char
,而不是输入字符:您可以使用输入ascii码。例如:(输入ascii码:41,字符为41)因为我可以把int转换成char。请帮忙again@jhoanne是的,你可以,我建议你弄清楚基本原理,如果你只是遵循这个方法,将来会给你带来麻烦
class prg1{
    public static void main(char a){
        int b=(int)a;
        System.out.println("ASCII value ="+b);
    }
}
public static void main(String[] args) {
    String a = "a";
    char b = a.charAt(0);
    int c = b;
    System.out.println(c);
}