Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(-) Symbol Used in Convert Method

Tags:

c#

I was going through some C# code and came upon this line:

Matrix[i, j] = Convert.ToInt32(grab[i, j] - '0');

What exactly does the ( - ) do??

What would be another way to write this if there is one?

like image 598
JLott Avatar asked Mar 11 '13 03:03

JLott


1 Answers

The - (minus) does exactly what it always does - subtracting. What happens here is subtracting the character code of zero '0' from the character at [i,j]. This converts a digit character to an integer value of the corresponding digit. For example, if you calculate

char digitChar = '7';
int digitVal = digitChar - '0';

the value of digitVal is seven.

like image 147
Sergey Kalinichenko Avatar answered Nov 05 '22 16:11

Sergey Kalinichenko