简体   繁体   English

c ++中的字符减法

[英]Char Subtraction in c++

I am fairly new to C++ and i have some trouble in understanding character subtraction in c++. 我对C ++很新,我在理解c ++中的字符减法方面遇到了一些麻烦。

I had this code intially 我有这个代码

char x='2';
x-='0';
if(x) cout << "More than Zero" << endl;

This returned More than Zero as output so to know the value of xi tried this code. 这返回超过零作为输出,以便知道xi的值尝试此代码。

char x='2';
x-='0';
if(x) cout << x << endl;

And i am getting null character(or new line) as output. 我得到null字符(或新行)作为输出。

Any help is appreciated. 任何帮助表示赞赏。

According to the C++ Standard (2.3 Character sets) 根据C ++标准(2.3字符集)

  1. ...In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous. ...在源和执行基本字符集中,上述十进制数字列表中0之后的每个字符的值应大于前一个值的值。

So the codes of adjacent digits in any character set differ by 1. 因此,任何字符集中相邻数字的代码相差1。

Thus in this code snippet 因此在此代码段中

char x='2';
x-='0';
if(x) cout << x << endl;

the difference between '2' and '0' (the difference between codes that represent these characters; for example in ASCII these codes are 0x32 and 0x30 while in EBCDIC they are 0xF2 and 0xF0 correspondingly) is equal to 2 . '2''0'之间的差异(表示这些字符的代码之间的差异;例如,在ASCII中,这些代码是0x32和0x30,而在EBCDIC中它们分别是0xF2和0xF0)等于2

You can check this for example the following way 您可以通过以下方式检查此项

if(x) cout << ( int )x << endl;

or 要么

if(x) cout << static_cast<int>( x ) << endl;

If you just write 如果你只是写

if(x) cout << x << endl;

then the operator << tries to output x as a printable character image of the value 2 because x is of type char . 然后运算符<<尝试输出x作为值2的可打印字符图像,因为xchar类型。

In C/C++ characters are stored as 8-bit integers with ASCII encoding. 在C / C ++中,字符存储为具有ASCII编码的8位整数。 So when you do x-='0'; 所以当你做x-='0'; you're subtracting the ASCII value of '0' which is 48 from the ASCII value of '2' which is 50. x is then equal to 2 which is a special control character stating STX (start of text), which is not printable. 你从'2'的ASCII值减去'0'的ASCII值是48,即50,然后它等于2,这是一个特殊的控制字符,表示STX(文本的开头),这是不可打印的。

If you want to perform arithmetic on characters it's better to subtract '0' from every character before any operation and adding '0' to the result. 如果要对字符执行算术运算,最好在任何操作之前从每个字符中减去“0”,并在结果中添加“0”。 To avoid problems like running over the range of the 8bit value I'd suggest to cast them on ints or longs. 为了避免像在8位值范围内运行这样的问题,我建议将它们放在整数或长整数上。

char x = '2';
int tempVal = x - '0';
/*
Some operations are performed here
*/
x = tempValue % 10 + '0'; 
// % 10 - in case it excedes the range reserved for numbers in ASCII
cout << x << endl;

It's much safer to perform these operations on larger value types, and subtracting the '0' character allows you to perform operations independent on the ASCII encoding like you'd do with casual integers. 在较大值类型上执行这些操作要安全得多,并且减去“0”字符允许您执行独立于ASCII编码的操作,就像使用偶然整数一样。 Then you add '0' to go back to the ASCII encoding, which alows you to print a number. 然后添加'0'返回ASCII编码,这将打印一个数字。

You are substracting 48 (ascii char '0') to the character 50 (ascii '2') 你将48(ascii char'0')减去字符50(ascii'2')

50 - 48 = 2
if (x) ' true

In C++, characters are all represented by an ASCII code (see http://www.asciitable.com/ ) I guess that doing : 在C ++中,字符都由ASCII代码表示(请参阅http://www.asciitable.com/ )我想这样做:

'2' - '0' '2' - '0'

is like doing 就像在做

50 - 48 = 2 50 - 48 = 2

According to the ASCII table, the ASCII code 2 stands for start of text , which is not displayed by cout. 根据ASCII表,ASCII码2代表文本的开头, cout不显示。

Hope it helps. 希望能帮助到你。

So what your code is doing is the following: 那么你的代码正在做的是以下内容:

x = '2', which represents 50 as a decimal value in the ASCII table. x ='2',表示在ASCII表中作为十进制值的50。

then your are basically saying: 那你基本上是在说:

x = x - '0', where zero in the ASCII table is represented as 48 in decimal, which equates to x = 50 - 48 = 2. x = x - '0',其中ASCII表中的零表示为十进制的48,等于x = 50 - 48 = 2。

Note that 2 != '2' . 请注意2!='2'。 If you look up 2(decimal) in the ASCII table that will give you a STX (start of text). 如果在ASCII表中查找2(十进制),它将为您提供STX(文本开头)。 This is what your code is doing. 这就是您的代码正在做的事情。 So keep in mind that the subtraction is taking place on the decimal value of the char. 所以请记住,减法是在char的十进制值上进行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM