0
    public class Try{
        public static void main(String args[]){
            String hex="11000010111100001001111010111000";
             String HexaBin="";
                for (int i = 0; i < hex.length(); i+=4) {
                    String str = hex.substring(i, i+4);
                    if (str=="0000")
                        HexaBin+= "0";
                    else if (str=="0001")
                        HexaBin+="1";
                    else if(str=="0010")
                        HexaBin+="2";
                    else if(str=="0011")
                        HexaBin+="3";
                    else if(str=="0100")
                        HexaBin+="4";
                    else if(str=="0101")
                        HexaBin+="5";
                    else if(str=="0110")
                        HexaBin+="6";
                    else if(str=="0111")
                        HexaBin+="7";
                    else if(str=="1000")
                        HexaBin+="8";
                    else if(str=="1001")
                        HexaBin+="9";
                    else if(str=="1010")
                        HexaBin+="A";
                    else if(str=="0000")
                        HexaBin+="B";
                    else if(str=="1100")
                        HexaBin+="C";
                    else if(str=="1101")
                        HexaBin+="D";
                    else if(str=="1110")
                        HexaBin+="E";
                    else if(str=="1111")
                        HexaBin+="F";

                }
                   System.out.println(HexaBin);
            }
        }

I have a string containing binary numbers. I need to convert it to Hexadecimal without using any built in function. I tried these codes but it wont run. I don't know why.

When I run it, it gets terminated. what's the problem with these codes.

user2768260
  • 29
  • 3
  • 6
  • 1
    What do you see when you step through the code in a debugger? I suspect this won't work if the binary is not a multiple of 4 digits long. BTW println(), length() and substring() are builtin functions. – Peter Lawrey Sep 13 '13 at 10:14
  • May we know why you don't want to use built in functions? – Code Enthusiastic Sep 13 '13 at 10:15
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Peter Lawrey Sep 13 '13 at 10:16

6 Answers6

2

use String.equals() instead of ==

if (str.equals("0000"))
  HexaBin+= "0";
else if (str.equals("0001"))
  HexaBin+="1";
...

Another point : it will not make you program fail but it is a bad practice to concatene Strings like this :

 HexaBin+="5";

You should rather declare your HexaBin as a StringBuilder and call StringBuilder.append():

//btw, variable names should begin with a lower case character.
StringBuilder hexaBin = new StringBuilder();
...
hexaBin.append("5");
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
1

Replace all == with the equals method in your if statements.

The == operator checks whether the references to the objects are equal or not while the equals method checks the actual contents.

JHS
  • 7,761
  • 2
  • 29
  • 53
1

Here is a different approach which may be interesting for you own use, but no professor will believe you wrote ;)

String hex = "11000010111100001001111010111000";
// to long
long val = 0;
for (char ch : hex.toCharArray())
    val = val * 2 + ch - '0';
String hexidecimal = "0123456789ABCDEF";
for (int i = (hex.length() - 1) / 4 * 4; i >= 0; i -= 4)
    System.out.print(hexidecimal.charAt((int) ((val >>> i) & 0xF)));
System.out.println();

prints

62F09EB8
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

String is object in java so use equals() Instead of ==

so Change to

if (str.equals("0000")) // for all cases
Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
0

Use equals() to compare two Strings for equality as String is a reference type.

Code Enthusiastic
  • 2,827
  • 5
  • 25
  • 39
0

As the other posters alredy said, compare Strings with equals(), not with ==.

Furthermore, you have a copy-paste-error in your code, "B" will never printed as you compare it with 0000 instead of 1011.

If you want to convert numbers between different radices, it's easier to use the functions on the number classes.

String hex = "11000010111100001001111010111000";

long num = Long.parseLong(hex, 2);
String hexaBin = Long.toString(num, 16).toUpperCase();
Peter Walser
  • 15,208
  • 4
  • 51
  • 78