Brian
Brian

Reputation: 67

Decimal to Hex help understanding

 import java.util.*;

public class Dec2Hex {
    public static void main (String[] args){
        Scanner input new Scanner (System.in);
        
        System.out.print("Enter a decimal number:  ");
        int decimal = input.nextInt();
        
        String hex = "";
        
        while (decimal != 0) {
            int hexValue = decimal % 16;
            char hexDigit = (0 <= hexValue && hexValue <= 9)?
               (char)(hexValue + '0'):  (char)(hexValue - 10 + 'A');
               
               hex = hexDigit + hex;
               decimal = decimal/16;
               
        }
        System.out.println("The hex number is  " + hex);
    }
}

This is an example in my book. I understand most of it but can't quite grasp a few parts. The example uses 1234 as the decimal entered.

  1. The remainder of 1234/16 is 2 so hexValue is 2. Therefore hexDigit is 2 + '0'. But hexDigit is a character so what is the meaning of combining or adding 2 and '0'?
  2. The final answer is 4D2. Where in the code did we ever provide instruction for any letter other than 'A'? How did it come up with a 'D'?

Thank you!

Upvotes: 0

Views: 232

Answers (5)

Veselin Perović
Veselin Perović

Reputation: 86

Ascii table is used for char type, so characters can be represented by decimal values. For example:

  • '0' in decimal 48
  • '1' in decimal 49
  • '2' in decimal 50
  • ...
  • '9' in decimal 57
  • 'A' in decimal 65
  • 'B' in decimal 66
  • 'C' in decimal 67
  • 'D' in decimal 68
  • ...
  • 'Z' in decimal 90
  • 'a' in decimal 97
  • 'b' in decimal 98
  • ...
  • 'z' in decimal 122

Having that in mind your here are the answers to your questions:

We focus on the line

char hexDigit = (0 <= hexValue && hexValue <= 9)?
           (char)(hexValue + '0'):  (char)(hexValue - 10 + 'A');
  1. The remainder of 1234/16 is 2 so hexValue is 2. Therefore hexDigit is 2 + '0'. But hexDigit is a character so what is the meaning of combining or adding 2 and '0'?

For this question the part of interest is

(char)(hexValue + '0')

The value hexValue is 2, so hexValue + '0' is 50, because in ascii table '0' has decimal value 48. Than value of 50 is casted to char, which by ascii table is '2'.

  1. The final answer is 4D2. Where in the code did we ever provide instruction for any letter other than 'A'? How did it come up with a 'D'?

For this question the part of interest is

(char)(hexValue - 10 + 'A')

The value hexValue is 13. By asci table 'A' has value of 65 and 'D' has value of 68. So hexValue - 10 is 3 and when we sum 3 and 'A' we get 68. Finally we do cast to char and get 'D'.

You can easyly test the answers by executing this piece of code:

public class Main {
public static void main(String[] args) {
    System.out.println("Decimal value: "+(2+'0')+"   Char value:" +(char)(2+'0'));
    System.out.println("Decimal value: "+(3 + 'A')+"   Char value: "+ (char)(3+'A'));
}

} Regards

Upvotes: 0

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79035

2 + '0' = 2 + 48 = 50
(char)50 = '2'

Note that

'0' = 48
'1' = 49
'2' = 50
...
'9' = 57

Check ASCII Table to learn more about ASCII values.

Demo:

public class Main {
    public static void main(String[] args) {
        System.out.println((char) 50);
    }
}

Output:

2

Upvotes: 0

Progman
Progman

Reputation: 19546

  1. The remainder of 1234/16 is 2 so hexValue is 2. Therefore hexDigit is 2 + '0'. But hexDigit is a character so what is the meaning of combining or adding 2 and '0'?

The value 2 is an integer value, however the variable type is char. No problem in assigning such a value to a char variable, however this would result in the 0x02 character/byte, which is a control character, not a visible character to display. The digits in the ASCII table to display begins at 0x30 (which is the digit '0'). To get the character value to save in the char variable you add your calculated value 2 with the offset 0x30. To make this calculation easier you use the expression '0'. This is the exact value of 0x30 (or 48 in decimal).

  1. The final answer is 4D2. Where in the code did we ever provide instruction for any letter other than 'A'? How did it come up with a 'D'?

The calculation is as follow:

  1. 1234 divided by 16 is 77 with a remainder of 2.
  2. 77 divided by 16 is 4 with a remainder of 13 (which is D).
  3. 4 divided by 16 is 0 with a remainder of 4.

This results in 4D2. To verify:

4*16*16 + 13*16 + 2 = 1234

Upvotes: 1

Shlomo Fel
Shlomo Fel

Reputation: 210

Every char has a value number between 0 to 256, each of these numbers encode a printable sign.

For example the value of '0' is 48, and the value of 'A' is 65.

So when adding a constant offset to a character it's like going to that offset in that encoding.

For example '0' + 5 is '5', or 'A' + 3 is 'D'.

So as for your questions:

  1. '0' + 2 is '2', i.e. the printable character of 2.
  2. In the code hexValue had the value of 13, so hexDigit got the value (hexValue - 10 + 'A'), which is (hexValue - 10 + 'A') = 13 - 10 + 'A' = 'A' + 3 = 'D'.

Upvotes: 0

Brian
Brian

Reputation: 67

I understand now that the second part of the condition is takin a number like "13", subtracting 10 and adding that to 'A'. 3 + 'A' is 'D' in unicode.

I still do not understand adding the '0' to the first part of the condition. Couldn't we just say (char)(hexValue)? Why add '0'?

Thanks!

Upvotes: 2

Related Questions