加减字符,为什么行得通? [英] Adding and subtracting chars, why does this work?

查看:51
本文介绍了加减字符,为什么行得通?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过"Java编程第9版简介"学习Java.丹尼尔·梁(Daniel Liang)在第9章我遇到了这段代码:

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

有人可以解释一下这里发生的事情吗?如何从整数中添加/减去字符,其背后的含义是什么?

Can someone explain what just happened in here? How is possible to add/subtract chars from integers and what's the meaning behind it?

推荐答案

来自

char数据类型是单个16位Unicode字符.

The char data type is a single 16-bit Unicode character.

char 由其代码点值表示:

  • min '\ u0000'(或0)
  • max:'\ uffff'(或65,535)
  • min '\u0000' (or 0)
  • max: '\uffff' (or 65,535)

您可以在 ASCII表上看到所有英文字母代码点.

You can see all of the English alphabetic code points on an ASCII table.

请注意, 0 == \ u0000 65,535 == \ uffff ,以及介于两者之间的所有内容.它们是对应的值.

Note that 0 == \u0000 and 65,535 == \uffff, as well as everything in between. They are corresponding values.

char 实际上只是存储为数字(其代码点值).我们有语法来表示像 char c ='A'; 这样的字符,但它等效于 char c = 65; 'A'== 65 是真的.

A char is actually just stored as a number (its code point value). We have syntax to represent characters like char c = 'A';, but it's equivalent to char c = 65; and 'A' == 65 is true.

因此,在您的代码中,字符用十进制值表示以进行算术运算(从0到65,535的整数).

So in your code, the chars are being represented by their decimal values to do arithmetic (whole numbers from 0 to 65,535).

例如,char 'A'由其代码点 65 (ASCII表中的小数)表示:

For example, the char 'A' is represented by its code point 65 (decimal value in ASCII table):

System.out.print('A'); // prints A
System.out.print((int)('A')); // prints 65 because you casted it to an int

请注意, short 是16位 signed 整数,因此,即使 char 也是16-位, char 的最大整数值(65,535)超过 short 的最大整数值(32,767).因此,从 char 强制转换为(short)不能总是有效.并且 char 的最小整数值为0,而 short 的最小整数值为-32,768.

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.

对于您的代码,假设 char 'D'.请注意,'D'== 68 ,因为它的代码点是 68 .

For your code, let's say that the char was 'D'. Note that 'D' == 68 since its code point is 68.

return 10 + ch - 'A';

这将返回 10 + 68-65 ,因此它将返回 13 .

This returns 10 + 68 - 65, so it will return 13.

现在假设字符是'Q'== 81 .

if (ch >= 'A' && ch <= 'F')

这是错误的,因为'Q'>'F'( 81> 70 ),因此它将进入 else 块并执行:

This is false since 'Q' > 'F' (81 > 70), so it would go into the else block and execute:

return ch - '0';

这将返回 81-48 ,因此它将返回 33 .

This returns 81 - 48 so it will return 33.

您的函数返回一个 int 类型,但是如果要返回一个 char 或将 int 强制转换为char 之后,则返回的 33 值将表示'!'字符,因为 33 是其代码点值.在ASCII表或Unicode表中查找字符以验证'!'== 33 (比较十进制值).

Your function returns an int type, but if it were to instead return a char or have the int casted to a char afterward, then the value 33 returned would represent the '!' character, since 33 is its code point value. Look up the character in ASCII table or Unicode table to verify that '!' == 33 (compare decimal values).

这篇关于加减字符,为什么行得通?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆