Reputation:
String s="00110001"
is a string containing the hexadecimal number 31
. What I want to know is how can I convert it to a string String a="1"
. (since the ASCII code for 1
is 49
and 49
in hexa is 31
.)
Upvotes: 0
Views: 58
Reputation: 1878
Try this:
String hex = "";
for(int i=0; i<=s.length() - 4; i+=4) {
hex += Integer.parseInt(s.substring(i, i+4), 2) + "";
}
System.out.println((char)Integer.parseInt(hex, 16));
Upvotes: 3
Reputation: 2828
You convert the number to decimal using the Integer.parseInt(string, radix) first.
int dec = Integer.parseInt(s, 2); //This will give you the value 49
System.out.println((char)dec);
Also, correction in your question: 00110001 is 31 in decimal
Upvotes: 2