Skip to content

Latest commit

 

History

History
40 lines (27 loc) · 1 KB

File metadata and controls

40 lines (27 loc) · 1 KB

Conversion to Integers

All chars have a matching numeric value1. 'a' is 97, 'b' is 98, '&' is 38, and so on.

Same as assigning an int to a double, you can perform a widening conversion by attempting to assign a char to an int.

~void main() {
int valueOfA = 'a';

IO.println(valueOfA);
~}

chars will be automatically converted to ints when used with mathmatical operators like +, -, >, <, etc.

~void main() {
char letter = 'A';
int result = letter + 3;

IO.println(result); // outputs 68

char gee = 'g';

// all the letters from a to z have consecutive numeric values.
boolean isLetter = gee >= 'a' && gee <= 'z';

IO.println(isLetter);
~}

This can be useful if you are stranded on Mars2 or if you want to see if a character is in some range.

Footnotes

  1. You can find some of these values in an "ASCII Table."

  2. https://www.youtube.com/watch?v=0xkP_FQUsuM