简体   繁体   English

字符串等效

[英]String equivalence

String s="00110001" is a string containing the hexadecimal number 31 . String s="00110001"是包含十六进制数字31的字符串。 What I want to know is how can I convert it to a string String a="1" . 我想知道的是如何将其转换为字符串String a="1" (since the ASCII code for 1 is 49 and 49 in hexa is 31 .) (由于用于ASCII码14949在六是31 )。

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));

You convert the number to decimal using the Integer.parseInt(string, radix) first. 您首先使用Integer.parseInt(string,radix)将数字转换为十进制。

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 此外,请在您的问题中更正:00110001为十进制31

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM