# Why 'Char' Value is equal to 48 'int' Value?

I was tinkering with my Arduino Board and I found Something Strange

int v;
char t = ‘2’;
void setup() {
Serial.begin(9600);
}

void loop() {
v=t;
Serial.print(t);
Serial.print(" ");
Serial.println(v);
}

And the Result were Strange the Char Value was ‘2’ but the Integer Value was ‘50’

Please explain me anyone why this happened??

Serial print sends the ASCII character codes. The ASCII characer code for ‘2’ is 5010 (0x32 hex). If you want to send the byte 2 use Serial.write(). That sends the binary number (0x02). If you want a number (2) displayed when received from a print (50 dec), subtract ‘0’ (0x30 or 40 dec) from the received character.

areeburrub:
And the Result were Strange the Char Value was '2' but the Integer Value was '50'

Please explain me anyone why this happened??

I was thinking that I should point out that subtracting ‘0’ only works with single characters, ‘0’ to ‘9’. So a better example is:

``````char receivedChar = '7';  // try this with a non numeric character, say 'e'

void setup()
{
Serial.begin(115200);

// check to maake sure that the receivedChar
// is a valid number 0 to 9.
{
int val = receivedChar - '0';
Serial.print("  val = ");
Serial.println(val);
}
else
{
Serial.println("invalid input");
}
}

void loop()
{

}
``````

areeburrub:
And the Result were Strange the Char Value was '2' but the Integer Value was '50'

the American Standards Association defined it in 1963 in the American Standard Code for Information Interchange (ASCII)

areeburrub:
And the Result were Strange the Char Value was '2' but the Integer Value was '50'

No, the integer value was 50.