简体   繁体   中英

Converting Hexadecimal to Decimal in Java

I am new to Stack Overflow, and I had some trouble with this program I am writing. My goal is to calculate the CRC32 hash of a file and display the result in both hexadecimal and decimal. The method that calculates the hex is fine, but when I try to convert to decimal the result is off.

eg The hex af3f89fc returns 2147483647 (wrong) when it should return 2940176892

Can anyone help with this method? I can't figure out what's wrong.

Side Note: The returned hex has lowercase letters.

public int toDecimal(String hex){
int dec = 0;
int len = hex.length();
for(int i = 0; i < len; i++){
  if(hex.charAt(i) == 'a') {
      dec += 11 * Math.pow(16, len-(i+1));
  }
  else if(hex.charAt(i) == 'b'){
      dec += 12 * Math.pow(16, len-(i+1));
  }
  else if(hex.charAt(i) == 'c'){
      dec += 13 * Math.pow(16, len-(i+1));
  }
  else if(hex.charAt(i) == 'd'){
      dec += 14 * Math.pow(16, len-(i+1));
  }
  else if(hex.charAt(i) == 'e'){
      dec += 15 * Math.pow(16, len-(i+1));
  }
  else{
      dec += Character.getNumericValue(hex.charAt(i)) * Math.pow(16, len-(i+1));
  }
}
return dec;
}

Thank you to whoever can help.

The value 2940176892 is somewhat bigger than the biggest possible int , which is the answer you're getting: 2147483647 .

This value, Integer.MAX_VALUE , occurs because you're adding the result of a multiplication between an int , eg 12 , and the result of Math.pow , which is a double .

You are using the operator += , which implicitly casts the result of the addition back to the type of the variable being assigned, according to the JLS, Section 15.26.2 , which in this case is an int .

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

According to the rules of the primitive narrowing conversion, JLS, Section 5.1.3 ), a value larger than Integer.MAX_VALUE is narrowed to Integer.MAX_VALUE .

  1. In the first step, the floating-point number is converted either to a long, if T is long, or to an int, if T is byte, short, char, or int, as follows:

(snip)

  • Otherwise, one of the following two cases must be true:

a. The value must be too small (a negative value of large magnitude or negative infinity), and the result of the first step is the smallest representable value of type int or long.

b. The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type int or long .

(emphasis mine)

Declare dec to be a long and declare toDecimal to return a long .

However, your character conversions are also incorrect. Convert:

'a' -> 10
'b' -> 11
'c' -> 12
'd' -> 13
'e' -> 14
'f' -> 15 (include this case!)

Then you'll get the correct answer of 2940176892.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM