比如:
通过a+'a'-'A' 将大写字母转换成小写字母
通过a+'A'-'a' 将小写字母转换成大写字母
这是为什么呢?
望点拨指教,谢谢!
ringa_lee2017-04-17 13:34:10
In order to distinguish different uses of values, the concept of type is added in the c/c++ language, so the same value is allowed to be interpreted in different ways;
λ ~/ cat a.cc
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[]) {
char c = 0b01000001;
int i = 0b01000001;
printf("char=%c\n", c);
printf("int =%d\n", i);
return EXIT_SUCCESS;
}
λ ~/ g++ a.cc -Wall
λ ~/ ./a.out
char=A #按照char来处理0b01000001会得到一个字符A
int =65 #按照int来处理0b01000001会得到一个数字65
When C++ handles the subtraction between chars, it is 按照原始的byte内二进制数值进行运算; 所以'a'可以和'A'做数值的加减
;
In addition, the ASCII code table defines a mapping relationship from a specific value to a human-readable character. See the ASCII table. You can see that the corresponding relationship is It is defined in alphabetical order, and at the specific value a>A; and the difference between a-A and b-B is fixed, 任何一个大写字母在数值上加上这个差值刚好就能得到对应的小写字母的值
.
二进制 十进制 十六进制 图形
01000001 65 41 A #65->A
01000010 66 42 B #66->B
01000011 67 43 C
01000100 68 44 D
...
01100001 97 61 a
01100010 98 62 b
01100011 99 63 c
01100100 100 64 d
...
黄舟2017-04-17 13:34:10
a + 'a' - 'A'
The ASCII of 'a' is 97, and the ASCII of 'A' is 65, so the above line is equal to a + 97 - 65
, that is, a + 32
.
PHPz2017-04-17 13:34:10
Give it a try:
printf("ASCII of c is : %d\nASCII of C is : %d\n", 'c', 'C');
PHP中文网2017-04-17 13:34:10
First of all, let’s clarify the concept of characters in C++. When actually stored, a variable of type char
is usually an 8-bit binary number (that is, a byte), so it supports various mathematical operations. When you use 'A'-'a'
, the result is the numerical difference between the two characters.
And how do a char
correspond to a number? This is about the ASCII character table, which clarifies the mapping relationship between characters and numbers. Each character corresponds to a number one by one. Part of the ASCII character table is as follows:
DEC OCT HEX BIN Symbol Description
...
48 060 30 00110000 0 Zero
49 061 31 00110001 1 One
50 062 32 00110010 2 Two
...
56 070 38 00111000 8 Eight
57 071 39 00111001 9 Nine
...
65 101 41 01000001 A Uppercase A
66 102 42 01000010 B Uppercase B
67 103 43 01000011 C Uppercase C
...
88 130 58 01011000 X Uppercase X
89 131 59 01011001 Y Uppercase Y
90 132 5A 01011010 Z Uppercase Z
...
97 141 61 01100001 a Lowercase a
98 142 62 01100010 b Lowercase b
99 143 63 01100011 c Lowercase c
...
120 170 78 01111000 x Lowercase x
121 171 79 01111001 y Lowercase y
122 172 7A 01111010 z Lowercase z
...
I wonder if the subject will be able to find the pattern after reading it? The characters in the table 0
to 9
are continuously distributed, A
to Z
are continuously distributed, and a
to z
are also continuously distributed. Moreover, the distance between the same pair of uppercase and lowercase letters is the same, that is, 'A'-'a'
, with uppercase letters in front and lowercase letters in the back.
So, you can convert uppercase letters to lowercase letters via ch + 'a' - 'A'
and lowercase letters to uppercase letters via ch + 'A' - 'a'
.
PHP中文网2017-04-17 13:34:10
Because when ASCII was designed at that time, when encoding letters, they were encoded very regularly:
The encoding of letters is sorted according to the order of the alphabet.
The encoding distance of uppercase and lowercase characters of the same letter is the same.
Suppose the inventor of ASCII had a fit at the beginning and encoded it completely at will. Then this method will not work.
In addition, the char type is actually an integer type, but it is designed to be directly assigned using character literals.