Wow, this is pretty wrong.. hexavalue 14 = 16^1*1 + 16^0*4 = 20 in dec.. but 20 in dec is NOT 1110 in bin.. It's 10100.
Integer.toBinaryString(i) needs a decimal number, but you put in 14 meaning hex.
The logic of conversion from Hexadecimal to Integer is wrong. 14 should have been converted into 10100 binary equivalent. And the binary 1110 is equivalent to E in Hexadecimal.
int i = Integer.parseInt(hex); should be
int i = Integer.parseInt(hex,16); to accomodate a-f
Change that and it works great - Thank you so much for the example.
Wrongnufan October 12, 2011 at 10:04 PM
Wow, this is pretty wrong.. hexavalue 14 = 16^1*1 + 16^0*4 = 20 in dec.. but 20 in dec is NOT 1110 in bin.. It's 10100. Integer.toBinaryString(i) needs a decimal number, but you put in 14 meaning hex.
Thanx!!java_p November 6, 2011 at 1:43 AM
thank u very much for this splendid piece of code!!
ConvertAlex January 25, 2012 at 3:38 PM
What about I enter a hexa value like: A, B, 9B ?
ConverterAlex January 25, 2012 at 3:42 PM
And one thing more, .toBinaryString() does not return first digits if this are "0". Integer.toBinaryString(3) return 11 not 0011.
Mistake in the outputShan February 19, 2012 at 3:54 PM
Hexadecimal 14 = Binary 10100
64 bit Hex to binary conversionMonesh Kumar February 24, 2012 at 3:07 PM
public class HexData { public static void main(String[] args) { String hexData = "0023456789ABCDEF"; long longData; String binData = null; int balZero = 0; System.out.println("Hex Data : " + hexData); longData = Long.parseLong(hexData, 16); System.out.println("Long Data : " + longData); binData = Long.toBinaryString(longData); balZero = (64 - (binData.length()-1)); for(int i=1;i<balZero;i++) { binData = "0" + binData; } System.out.println("Bin Data : " + binData); } }
Rectify mistake Manjeet Harsh March 29, 2012 at 2:03 AM
The logic of conversion from Hexadecimal to Integer is wrong. 14 should have been converted into 10100 binary equivalent. And the binary 1110 is equivalent to E in Hexadecimal.
ErrorVibhav Sinha August 5, 2012 at 9:45 PM
How can you parseInt a String having non digit characters. It is giving error.
Hexidecimal to Binary Conversion FixPancho Delgado August 10, 2012 at 8:20 AM
int i = Integer.parseInt(hex); should be int i = Integer.parseInt(hex,16); to accomodate a-f Change that and it works great - Thank you so much for the example.
this is longdfjk August 23, 2012 at 3:36 PM
Is it??
Post your Comment