十进制转十六进制帮助理解

huangapple go评论75阅读模式
英文:

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);
    }
}

这是你提供的代码内容。下面是翻译的结果:

import java.util.*;

public class Dec2Hex {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("请输入一个十进制数:");
        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("十六进制数为:" + hex);
    }
}
英文:
 import java.util.*;

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

答案1

得分: 2

我现在明白了,条件的第二部分是将一个类似于"13"的数字,减去10,然后加上'A'。 3 + 'A'在Unicode中表示为'D'。

我仍然不明白为什么要在条件的第一部分中加上'0'。我们不能只是写成(char)(hexValue)吗?为什么要加上'0'?

谢谢!

英文:

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!

答案2

得分: 1

  1. 余数为1234/16的余数为2,因此hexValue为2。因此hexDigit为2 + '0'。但hexDigit是一个字符,那么将2和'0'相结合或相加的意义是什么?

2 是一个整数值,然而变量类型是 char。将这样的值赋给 char 变量没有问题,但这会得到字符/字节 0x02,这是一个控制字符,不是可见的显示字符。ASCII 表中用于显示的数字从 0x30 开始(即数字 &#39;0&#39;)。为了获得要保存在 char 变量中的字符值,您将计算出的值 2 与偏移量 0x30 相加。为了更轻松地进行这个计算,您使用表达式 &#39;0&#39;。这恰好是 0x30 的确切值(或十进制中的 48)。

  1. 最终答案是4D2。在代码中,我们除了 'A' 之外的任何字母都没有提供指令。它是如何得出 'D' 的?

计算如下:

  1. 1234 除以 16 得到商 77,余数为 2
  2. 77 除以 16 得到商 4,余数为 13(即 D)。
  3. 4 除以 16 得到商 0,余数为 4

这导致结果为 4D2。验证方式如下:

4*16*16 + 13*16 + 2 = 1234
英文:

> 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 &#39;0&#39;). 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 &#39;0&#39;. This is the exact value of 0x30 (or 48 in decimal).

> 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'?

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

答案3

得分: 0

每个字符都有一个值介于0到256之间的数字,其中每个数字都编码了一个可打印的符号。

例如,字符'0'的值为48,字符'A'的值为65。

因此,当对字符添加一个固定的偏移量时,就相当于在该编码中前进了该偏移量。

例如,'0' + 5就是'5',或者'A' + 3就是'D'。

所以针对你的问题:

  1. '0' + 2等于'2',即数字2的可打印字符。
  2. 在代码中,hexValue的值为13,所以hexDigit得到的值为(hexValue - 10 + 'A'),这是(13 - 10 + 'A') = 'A' + 3 = 'D'。
英文:

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'.

答案4

得分: 0

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

Note that 

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

 Check [ASCII Table](http://www.asciitable.com/) to learn more about ASCII values.

**Demo:**

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

2
英文:
2 + &#39;0&#39; = 2 + 48 = 50
(char)50 = &#39;2&#39;

Note that

&#39;0&#39; = 48
&#39;1&#39; = 49
&#39;2&#39; = 50
...
&#39;9&#39; = 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

答案5

得分: 0

Ascii表 用于字符类型,因此字符可以用十进制值表示。例如:

  • '0' 的十进制值为 48
  • '1' 的十进制值为 49
  • '2' 的十进制值为 50
  • ...
  • '9' 的十进制值为 57
  • 'A' 的十进制值为 65
  • 'B' 的十进制值为 66
  • 'C' 的十进制值为 67
  • 'D' 的十进制值为 68
  • ...
  • 'Z' 的十进制值为 90
  • 'a' 的十进制值为 97
  • 'b' 的十进制值为 98
  • ...
  • 'z' 的十进制值为 122

记住这一点,以下是你问题的答案:

我们关注这一行代码:

char hexDigit = (0 <= hexValue && hexValue <= 9) ?
               (char)(hexValue + '0') : (char)(hexValue - 10 + 'A');
  1. 1234 除以 16 的余数是 2,所以 hexValue 是 2。因此 hexDigit 是 2 + '0'。但 hexDigit 是一个字符,那么将 2 和 '0' 结合或相加的意义是什么?

对于这个问题,感兴趣的部分是:

(char)(hexValue + '0')

hexValue 是 2,所以 hexValue + '0' 是 50,因为在 ASCII 表中 '0' 的十进制值为 48。然后将值 50 转换为字符,根据 ASCII 表,它是 '2'。

  1. 最终答案是 4D2。在代码中,除了 'A' 之外,我们从未提供任何其他字母的指令。它怎么会得出 'D'?

对于这个问题,感兴趣的部分是:

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

hexValue 是 13。根据 ASCII 表,'A' 的值是 65,'D' 的值是 68。所以 hexValue - 10 是 3,当我们将 3 与 'A' 相加时,得到 68。最后我们将其转换为字符,得到 'D'。

你可以通过执行这段代码轻松测试这些答案:

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'));
    }
}

祝好,

英文:

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 &lt;= hexValue &amp;&amp; hexValue &lt;= 9)?
           (char)(hexValue + &#39;0&#39;):  (char)(hexValue - 10 + &#39;A&#39;);

> 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 + &#39;0&#39;)

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'.

> 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'?

For this question the part of interest is

(char)(hexValue - 10 + &#39;A&#39;)

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(&quot;Decimal value: &quot;+(2+&#39;0&#39;)+&quot;   Char value:&quot; +(char)(2+&#39;0&#39;));
	System.out.println(&quot;Decimal value: &quot;+(3 + &#39;A&#39;)+&quot;   Char value: &quot;+ (char)(3+&#39;A&#39;));
}

}
Regards

huangapple
  • 本文由 发表于 2020年10月10日 06:27:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64288062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定