[英]how to convert from decimal to ASCII characters
我试图编写一个函数,将字符串中的最后两个十六进制转换为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;
}
您非常接近:您所需Integer.toString
的只是Integer.toString
而不是调用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;
对不起,我的英语不好。 这就是我解决这个问题的方法strI = Integer.toString(hex);
这句话是错误的。它使十六进制成为字符串。例如,如果hex = 97,StrI =“ 97”而不是“ a”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.