title: Hexadecimal to Integer in Java
body: <p>I am trying to convert a String hexadecimal to an integer. The string hexadecimal was calculated from a hash function (sha-1). I get this error : java.lang.NumberFormatException. I guess it doesn't like the String representation of the hexadecimal. How can I achieve that. Here is my code : </p>
  
  <pre><code>public Integer calculateHash(String uuid) {
  
      try {
          MessageDigest digest = MessageDigest.getInstance("SHA1");
          digest.update(uuid.getBytes());
          byte[] output = digest.digest();
  
          String hex = hexToString(output);
          Integer i = Integer.parseInt(hex,16);
          return i;           
  
      } catch (NoSuchAlgorithmException e) {
          System.out.println("SHA1 not implemented in this system");
      }
  
      return null;
  }   
  
  private String hexToString(byte[] output) {
      char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
              'A', 'B', 'C', 'D', 'E', 'F' };
      StringBuffer buf = new StringBuffer();
      for (int j = 0; j &lt; output.length; j++) {
          buf.append(hexDigit[(output[j] &gt;&gt; 4) &amp; 0x0f]);
          buf.append(hexDigit[output[j] &amp; 0x0f]);
      }
      return buf.toString();
  
  }
  </code></pre>
  
  <p>For example, when I pass this string : <strong>_DTOWsHJbEeC6VuzWPawcLA</strong>, his hash his : <strong>0xC934E5D372B2AB6D0A50B9F0341A00ED029BDC15</strong> </p>
  
  <p>But i get : java.lang.NumberFormatException: For input string: "<strong>0xC934E5D372B2AB6D0A50B9F0341A00ED029BDC15</strong>"</p>
  
  <p>I really need to do this. I have a collection of elements identified by their UUID which are string. I will have to store those elements but my restrictions is to use an integer as their id. It is why I calculate the hash of the parameter given and then I convert to an int. Maybe I am doing this wrong but can someone gives me an advice to achieve that correctly!!</p>
  
  <p>Thanks for your help !!</p>