How can I convert a Hex String into 32bit Binary String? I did
String binAddr = Integer.toBinaryString(Integer.parseInt(hexAddr, 16));
To get the Binary String, but I need to pad it with 0's to ensure its 32 bits, how can I do that, preferably with Formatter?
String binAddr = Integer.toBinaryString(Integer.parseInt(hexAddr, 16));
String.format("%032", new BigInteger(binAddr));
The idea here is to parse the string back in as a decimal number temporarily (one that just so happens to consist of all 1's and 0's) and then use String.format()
.
Note that you basically have to use BigInteger, because binary strings quickly overflow Integer and Long resulting in NumberFormatExceptions if you try to use Integer.fromString()
or Long.fromString()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.