• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hex to Binary run time error  RSS feed

 
Steve Pickle
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am have piece of code which convert Hex value 16 character  to binary.
---------------------------------------------------------------------------------------------------------
76     String unpaddedBinary = new BigInteger("1" + hex,16).toString(2).substring(1);
77    String paddedBinary = String.format("%064s", Integer.parseInt(unpaddedBinary, 2));
78    System.out.println(paddedBinary);

---------------------------------------------------------------------------------------------------------

hex value 7024058020C01801

Getting below error at run time at line 77. How to get rid of it, its giving me correct out put.

Exception in thread "main" java.lang.NumberFormatException: For input string: "0
111000000100100000001011000000000100000110000000001100000000000"
        at java.lang.NumberFormatException.forInputString(Unknown Source)
        at java.lang.Integer.parseInt(Unknown Source)
        at DecodeAudit.main(DecodeHex.java:77)


 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
brain jv wrote:
Getting below error at run time at line 77. How to get rid of it, its giving me correct out put.

Exception in thread "main" java.lang.NumberFormatException: For input string: "0
111000000100100000001011000000000100000110000000001100000000000"
        at java.lang.NumberFormatException.forInputString(Unknown Source)
        at java.lang.Integer.parseInt(Unknown Source)
        at DecodeAudit.main(DecodeHex.java:77)


Java integers are 32 bit in length (including the sign bit). What you have is much longer than 32 bits.

Henry
 
Carey Brown
Bartender
Posts: 3016
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
parseInt() returns an int and your printf() format ("%s") is attempting to print a String.
 
Steve Pickle
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where to change the code to get rid of error.
 
Carey Brown
Bartender
Posts: 3016
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not
 
Campbell Ritchie
Marshal
Posts: 55789
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Since a hex digit always corresponds to four binary digits, there must be an easier way to do that than parsing into a number. Are you doing arithmetic with those numbers? If not you can leave them as text. I would suggest you create yourself BinaryNumber and HexNumber classes. They can have toBinary and toHex methods to convert between the two classes. There are all sorts of ways you can convert a single letter to a four‑element String. You can use a method of the Character class and keep the binary in an array. You can use a Map with one of the numbers as the key and the other as the value. You can even try to find a bidirectional Map which can convert both ways.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!