[英]How to convert a String consisting of Binary Number to Hexa Decimal no in Java?
My String
is 010101010111111111101010101010101010111101010101010101010101
and it is large in size (more than 64 characters). 我的String
是010101010111111111101010101010101010111101010101010101010101
,它的大小很大(超过64个字符)。
I cannot use Integer
or Long
class parse methods due to size limitation. 由于大小限制,我无法使用Integer
或Long
类解析方法。
Expected Output would be 557FEAAAAF55555h
. 预期输出为557FEAAAAF55555h
。
I don't know your performance requirements, but it seems to me you could simply: 我不知道您对性能的要求,但是在我看来,您可以简单地:
Use BigInteger
for this: 为此使用BigInteger
:
String s = "010101010111111111101010101010101010111101010101010101010101";
BigInteger value = new BigInteger(s, 2);
System.out.println(value.toString(16));
This shows: 由此可见:
557feaaaaf55555
Or to get your exact output: 或获得您的确切输出:
System.out.println(value.toString(16).toUpperCase() + "h");
public String GetHex(String binary){
String[] parts=binary.split("(?<=\\G.{4})");
String output="";
for(String s: parts)
{
output+=Integer.toHexString(Integer.parseInt(s, 2));
}
}
And Call it with your Binary String 并使用您的二进制字符串进行调用
String binary="010101010111111111101010101010101010111101010101010101010101";
String hexvalue=GetHex(binary); //557feaaaaf55555
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.