1

I have homework to convert from BIN to HEX and I wrote the following algo code:

import java.util.Scanner;

public class BinaryToHex {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Binary number: ");
        String b = input.next();
        int bin = Integer.parseInt(b);
        int arrlength = b.length();

        while (arrlength%4  !=  0){
            arrlength++;
        }

        int[] arrbin =  new int [arrlength];
        int digit = 0;
        String hex = "";
        String str;
        int conv;

        for (int i = arrlength-1; i>=0; i--){
            digit = bin%10;
            arrbin[i]=digit;
            bin = bin/10;
        }

        System.out.print("Hex value = ");

        for (int index = 0; index < arrlength; index=index+4){
            str = "" + arrbin[index] + "" + arrbin[index+1] + "" + arrbin[index+2] + "" + arrbin[index+3];
            switch(str){
            case "0000": str = "0"; break;
            case "0001": str = "1"; break;
            case "0010": str = "2"; break;
            case "0011": str = "3"; break;
            case "0100": str = "4"; break;
            case "0101": str = "5"; break;
            case "0110": str = "6"; break;
            case "0111": str = "7"; break;
            case "1000": str = "8"; break;
            case "1001": str = "9"; break;
            case "1010": str = "A"; break;
            case "1011": str = "B"; break;
            case "1100": str = "C"; break;
            case "1101": str = "D"; break;
            case "1110": str = "E"; break;
            case "1111": str = "F"; break;
            }
            System.out.print(str);
        }
    }

}

The problem is that when I am trying to convert bigger numbers it throws:

Exception in thread "main" java.lang.NumberFormatException: For input string: "10101010101010"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at BinaryToHex.main(BinaryToHex.java:9)

I know that the problem is connected with the int type, but I cannot figure out how to solve this. I tried to use long type - the result was same.

I would be grateful if you guys, can help me to correct this code, in order to work with bigger numbers.

5 Answers 5

3

In Java the int is a signed 32 bit so its range is

-2,147,483,648 to 2,147,483,647

So your value of 10101010101010 is outside of this range.

Try using something larger like a Long

long bin = Long.valueOf("10101010101010");
System.out.println(bin);

See Primitive Data Types

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it works ! That's what I've been searching for !I regret I am not allowed to vote up for you ... :|
0

This number is indeed too big. Use Long.parseLong() and long type instead of int if you need such big numbers.

EDIT:

I have just understood that you actually want to parse binary number. So, use Integer.parseInt(str, 2). Otherwise the number is interpreted as decimal.

Comments

0

I used this method for conversion

   public static String binaryToHex(String bin) {
   return String.format("%21X", Long.parseLong(bin,2)) ;
}

Comments

0

I did it quick and dirty. Should work for all lengths:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Binary number: ");
    String b = input.next();
    while (b.length() % 4 != 0) b = "0" + b;
    StringBuilder builder = new StringBuilder();
    for (int count = 0; count < b.length(); count += 4) {
        String nibble = b.substring(count, count + 4);
        builder.append(Integer.toHexString(Integer.parseInt(nibble, 2)));
    }
    System.out.println(builder);
}

Comments

0

If the input String is at maximum a 32 bit value there is no need to use a Long.

-- for any binary input till 32 bit lenght following will work
Integer.parseInt(yourBinaryString, 2)

-- for any binary input till 64 bit lenght following will work
Long.parseLong(yourBinaryString, 2)

-- for longer binary string input values have a look at BigInteger

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.