Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.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
Java 如何将十进制字符转换为ASCII字符_Java - Fatal编程技术网

Java 如何将十进制字符转换为ASCII字符

Java 如何将十进制字符转换为ASCII字符,java,Java,我正在尝试编写一个函数,将字符串中的最后两个十六进制转换为ASCII字符。如“ab3A”应打印“ab:” 这是我写的代码,它将最后两个转换为十进制,但无法将十进制转换为ASCII字符。我试图使用.toString()来完成它,但没有成功 private static String unmangle(String word) { String newTemp = word.substring(word.indexOf('%')+1); int hex = hexTo

我正在尝试编写一个函数,将字符串中的最后两个十六进制转换为ASCII字符。如“ab3A”应打印“ab:”

这是我写的代码,它将最后两个转换为十进制,但无法将十进制转换为ASCII字符。我试图使用.toString()来完成它,但没有成功

 private static String unmangle(String word)
 {      
    String newTemp = word.substring(word.indexOf('%')+1);
    int hex = hexToInt(newTemp);
    String strI = Integer.toString(hex);

    System.out.println(strI);

    word=word.replace("%", "");
    word=word.replace("+", " ");
    return word = word.replace(newTemp, "")+ strI;


}

您非常接近:您只需要一个cast,而不是调用
Integer.toString
-

private static String unmangle(String word)
{      
    String newTemp = word.substring(word.indexOf('%')+1);
    char hex = (char)hexToInt(newTemp);

    word=word.replace("%", "");
    word=word.replace("+", " ");
    return word = word.replace(newTemp, "")+ hex;
}

首先,您应该知道如何将十六进制转换为ASCII

String newTemp=word.substring(word.length-1,word.length);
char a=newTemp.substring(0,0);
char b=newTemp.substring(1,1);
int c=0,d=0;
//And you should convert to int.
if(a='A'|'a')
c=10;//the same to d.
//and 
c=c*16+d;
String strI = Integer.toString(c);
return word.substring(0,word.length-2)+strI;
很抱歉我的英语不好。这是我处理这个问题的方法


这个句子是错误的。它将十六进制转换为字符串。例如,如果十六进制=97,StrI=“97”而不是“a”

听起来像是您真正想要的,java.lang.string)您可能应该说“.最后两个字符…”,或者“.最后两个数字…”,而不是使用上面的“decimal”一词。事实上,正如您的代码所述,您正在从“十六进制”(十六进制的缩写)转换为整数。(但转换成十进制会把事情搞砸。)为什么要重新发明
java.net.urldecover?