加法和减法字符为什么这有效? (Java)的 [英] adding and subtraction chars why does this work? (java)
问题描述
我正在通过Daniel liang在第9章字符串的Java编程第9版简介中学习Java我遇到过这段代码:
public static int hexCharToDecimal(char ch){
if(ch> ='A'&& ch< ='F')
返回10 + ch - 'A';
其他
返回ch - '0';
}
oki所以有人可以解释刚刚发生在这里的事情吗?怎么可能添加,整数的子字符和它背后的意义和感谢。
专注于我的即使我已经包含了Unicode字符,也讨论了ASCII值。
来自文档
char数据类型是单个16位Unicode字符。
char可以表示为:
- 其代码点值(Unicode) - min:
'\ u0000'
和max:'\ uffff'
- 其 ASCII值 min:0和最大值:65,535(含)
注意 0 == \ u0000
和 65,535 == \ airff
- 以及介于两者之间的所有内容。它们是相应的值。
在你的代码中,字符由它们的ASCII值表示来进行算术运算(整数从0到65,535)。 / p>
例如,char 'A'
可以用其int(ASCII)值 65
:
System.out.print('A'); //打印A
System.out.print((int)('A'))//打印65因为你将它转换为int
作为注释, short
是一个16位签名的整数,所以即使 char
也是16位, char
(65,535)的最大整数值超过最大值 short的整数值
(32,767)。因此,从 char
转换为(短)
并不总是有效。并且 char
的最小整数值为0,而 short
的最小整数值为-32,768。 / sub>
对于你的代码,假设char是'D'
返回10 + ch - 'A';
将返回 10 + 68 - 65
所以它将返回 13
对于所有意图和目的,它转换 char
数据类型为整数值,然后进行基本算术运算。 'D'
char可以用其整数ASCII值 68表示
假设char是'H'
if(ch> ='A'&& ch< ='F')不成立,因为''H'`> ''F'`(72> 70)
所以它会
return ch - '0';
将返回 72 - 48
所以它将返回 24
I’m learning Java through "introduction to Java programming 9th edition" by Daniel liang at chapter 9 "strings" I’ve encountered this piece of code :
public static int hexCharToDecimal(char ch){
if (ch >= 'A' && ch <= 'F')
return 10 + ch - 'A';
else
return ch - '0';
}
oki so can someone explain what just happened in here? how is possible to add,sub chars from integers and what's the meaning behind it and thanks.
Focus on my discussion of the ASCII value even though I have included Unicode character.
From the Docs
The char data type is a single 16-bit Unicode character.
A char can be represented by:
- its code point value (Unicode) - min:
'\u0000'
and max:'\uffff'
- its ASCII value min: 0 and max: 65,535 (inclusive)
Note that 0 == \u0000
and 65,535 == \uffff
- as well as everything in between. They are corresponding values.
In your code, the chars are being represented by their ASCII value to do arithmetic (whole numbers from 0 to 65,535).
For example, the char 'A'
can be represented by its int (ASCII) value 65
:
System.out.print('A'); // prints A
System.out.print( (int)('A') ) // prints 65 because you casted it to an int
As a note, a short
is a 16-bit signed integer, so even though a char
is also 16-bits, the maximum integer value of a char
(65,535) exceeds the maximum integer value of a short
(32,767). Therefore, a cast to (short)
from a char
cannot always work. And the minimum integer value of a char
is 0, whereas the minimum integer value of a short
is -32,768.
For your code, let's say that the char was 'D'
return 10 + ch - 'A';
will return 10 + 68 - 65
so it will return 13
For all intents and purposes, it "converts" the char
data types to integer values and then does basic arithmetic. The 'D'
char can be represented by its integer ASCII value of 68
Let's say the char was 'H'
if (ch >= 'A' && ch <= 'F') would not be true because `'H'` > `'F'` (72 > 70)
so it would
return ch - '0';
which would return 72 - 48
so it will return 24
这篇关于加法和减法字符为什么这有效? (Java)的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!