title: Converting binary string to a hexadecimal string JAVA
body: <p>I want to convert my binary(which is in string) to hexadecimal string also, this is just a program fragment since this program is just a part of another bigger program:</p>
  
  <pre><code>//the variable name of the binary string is: "binary"
  int digitNumber = 1;
      int sum = 0;
      int test = binary.length()%4;
      if(test!=0) {
          binary = padLeft(binary, test);
      }
      for(int i = 0; i &lt; binary.length(); i++){
          if(digitNumber == 1)
              sum+=Integer.parseInt(binary.charAt(i) + "")*8;
          else if(digitNumber == 2)
              sum+=Integer.parseInt(binary.charAt(i) + "")*4;
          else if(digitNumber == 3)
              sum+=Integer.parseInt(binary.charAt(i) + "")*2;
          else if(digitNumber == 4 || i &lt; binary.length()+1){
              sum+=Integer.parseInt(binary.charAt(i) + "")*1;
              digitNumber = 0;
              if(sum &lt; 10)
                  System.out.print(sum);
              else if(sum == 10)
                  System.out.print("A");
              else if(sum == 11)
                  System.out.print("B");
              else if(sum == 12)
                  System.out.print("C");
              else if(sum == 13)
                  System.out.print("D");
              else if(sum == 14)
                  System.out.print("E");
              else if(sum == 15)
                  System.out.print("F");
              sum=0;
          }
          digitNumber++;  
      }
      public static String padLeft(String s, int n) {
          return String.format("%0$"+n+"s", s);
      }//i added this for padding
  </code></pre>
  
  <p>the problem is that i dont know if the padding works but i am sure that this program return a wrong hexadecimal conversion of the binary string I am trying to do this: </p>
  
  <p><a href="http://www.wikihow.com/Convert-Binary-to-Hexadecimal">http://www.wikihow.com/Convert-Binary-to-Hexadecimal</a></p>
  
  <p><strong>PS: I need to implement it(not using any built-in function)</strong></p>