ASCII Fundamentals
Character data in computing systems is stored as numeric values using the ASCII (American Standard Code for Information Interchange) encoding scheme. This representation enables flexible operations between character and integer types, which proves valuablee in competitive programming and general application development.
Retrieving ASCII Values
C++ Implementation:
#include <iostream>
using namespace std;
int main() {
char letter = 'G';
char space = ' ';
cout << static_cast<int>(letter) << endl; // outputs 71
cout << static_cast<int>(space) << endl; // outputs 32
return 0;
}
The static_cast() operator performs the type conversion. Without this cast, cout outputs the character itself rather than its numeric representation.
Python Implementation:
# Using built-in ord() function
print(ord('M')) # outputs 77
print(ord('#')) # outputs 35
Java Implementation:
public class AsciiConverter {
public static void main(String[] args) {
char symbol = '@';
char digit = '7';
System.out.println((int) symbol); // outputs 64
System.out.println((int) digit); // outputs 55
}
}
Character Comparison
C++ Comparison:
#include <iostream>
using namespace std;
int main() {
char x = 'p';
int value = 110;
cout << (x < value) << endl; // outputs 1 (true)
cout << ('b' != 98) << endl; // outputs 1 (true)
return 0;
}
C++ performs implicit conversion when comparing char with int types.
Python Comparison:
# Python enforces strict type checking
print('p' > 'q') # False - direct character comparison
# Converting between types for comparison
print(chr(112) < 'q') # True
print(ord('p') == 113) # False - p is 112
Java Comparison:
public class CharComparison {
public static void main(String[] args) {
// Direct comparison using operators
System.out.println('x' == 'y'); // false
// Using Character class method
int result = Character.compare('a', 'c');
System.out.println(result); // outputs -2
// Result interpretation: negative means first < second
// zero means equal, positive means first > second
}
}
Digit Character Detection
ASCII numeric characters span the range 48 through 57 (representing '0' through '9').
C++ Detection:
#include <iostream>
#include <cctype>
using namespace std;
int main() {
char ch = '5';
// Range-based check
bool isDigit = (ch >= '0' && ch <= '9');
cout << isDigit << endl; // 1 (true)
// Using standard library function
cout << isdigit(ch) << endl; // 1 (true)
cout << isalpha(ch) << endl; // 0 (false)
return 0;
}
}
Python Detection:
test_char = '9'
# Range-based detection
result = '0' <= test_char <= '9'
print(result) # True
# Using string methods
print(test_char.isdigit()) # True
print(test_char.isalpha()) # False
Java Detection:
public class DigitChecker {
public static void main(String[] args) {
char testChar = 'A';
// Range-based approach
boolean inRange = (testChar >= '0' && testChar <= '9');
System.out.println(inRange); // false
// Using Character class methods
System.out.println(Character.isDigit(testChar)); // false
System.out.println(Character.isLetter(testChar)); // true
System.out.println(Character.isUpperCase(testChar)); // true
}
}
Case Conversion Techniques
The ASCII relationship between uppercase and lowercase letters involves a constant offset of 32:
- 'A' = 65, 'a' = 97
- 'Z' = 90, 'z' = 122
C++ Case Conversion:
#include <iostream>
using namespace std;
int main() {
char upper = 'K';
char lower = 'p';
// Upper to lower
char toLower = upper + 32;
cout << toLower << endl; // outputs 'k'
// Lower to upper
char toUpper = lower - 32;
cout << toUpper << endl; // outputs 'P'
return 0;
}
Python Case Conversion:
upper_letter = 'Q'
lower_letter = 'g'
# Convert using chr() and ord()
converted_down = chr(ord(upper_letter) + 32)
print(converted_down) # 'q'
converted_up = chr(ord(lower_letter) - 32)
print(converted_up) # 'G'
# Alternatively using string methods
print(lower_letter.upper()) # 'G'
print(upper_letter.lower()) # 'q'
Java Case Conversion:
public class CaseConverter {
public static void main(String[] args) {
char upCase = 'M';
char lowCase = 't';
// Using arithmetic offset
char toLowerCase = (char)(upCase + 32);
System.out.println(toLowerCase); // 'm'
char toUpperCase = (char)(lowCase - 32);
System.out.println(toUpperCase); // 'T'
// Using Character class
System.out.println(Character.toLowerCase(upCase)); // 'm'
System.out.println(Character.toUpperCase(lowCase)); // 'T'
}
}
Numeric Character Conversions
Converting between numeric characters and their integer values:
C++ Conversion:
#include <iostream>
using namespace std;
int main() {
int num = 7;
char digit = '4';
// Integer to character digit
char fromInt = num + '0';
cout << fromInt << endl; // outputs '7'
// Character digit to integer
int fromChar = digit - '0';
cout << fromChar << endl; // outputs 4
return 0;
}
}
Python Conversion:
numeric_value = 6
char_digit = '3'
# Integer to character
result_char = chr(numeric_value + ord('0'))
print(result_char) # '6'
# Character to integer
result_num = ord(char_digit) - ord('0')
print(result_num) # 3
# Alternative using int()
print(int(char_digit)) # 3
Java Conversion:
public class NumericConversion {
public static void main(String[] args) {
int number = 9;
char digitChar = '2';
// Integer to character
char fromNumber = (char)(number + '0');
System.out.println(fromNumber); // '9'
// Character to integer
int fromChar = digitChar - '0';
System.out.println(fromChar); // 2
// Using parsing
int parsed = Character.digit(digitChar, 10);
System.out.println(parsed); // 2
}
}