Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hexadecimal to Integer in Java

Tags:

java

integer

hex

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 :

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 < output.length; j++) {         buf.append(hexDigit[(output[j] >> 4) & 0x0f]);         buf.append(hexDigit[output[j] & 0x0f]);     }     return buf.toString();  } 

For example, when I pass this string : _DTOWsHJbEeC6VuzWPawcLA, his hash his : 0xC934E5D372B2AB6D0A50B9F0341A00ED029BDC15

But i get : java.lang.NumberFormatException: For input string: "0xC934E5D372B2AB6D0A50B9F0341A00ED029BDC15"

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!!

Thanks for your help !!

like image 721
Dimitri Avatar asked May 04 '11 16:05

Dimitri


People also ask

How do you convert hex to integer?

To convert a hexadecimal string to a numberUse the ToInt32(String, Int32) method to convert the number expressed in base-16 to an integer. The first argument of the ToInt32(String, Int32) method is the string to convert. The second argument describes what base the number is expressed in; hexadecimal is base 16.

What is hexadecimal integer in Java?

For Hexadecimal, the 0x or 0X is to be placed in the beginning of a number. Note − Digits 10 to 15 are represented by a to f (A to F) in Hexadecimal. Here are some of the examples of hexadecimal integer literal declared and initialized as int.

Does Java support hexadecimal?

In Java programs, hexadecimal numbers are written by placing 0x before numbers.


1 Answers

Why do you not use the java functionality for that:

If your numbers are small (smaller than yours) you could use: Integer.parseInt(hex, 16) to convert a Hex - String into an integer.

  String hex = "ff"   int value = Integer.parseInt(hex, 16);   

For big numbers like yours, use public BigInteger(String val, int radix)

  BigInteger value = new BigInteger(hex, 16); 

@See JavaDoc:

  • Integer.parseInt(String value, int radix)
  • BigInteger(String value, int radix)
like image 175
Ralph Avatar answered Sep 18 '22 13:09

Ralph