Skip to content

Commit 61c87e4

Browse files
committed
Add ascii_to_char conversion script
1 parent c79034c commit 61c87e4

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

conversions/ascii_to_char.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Convert a given ASCII value to its corresponding character.
3+
"""
4+
5+
def ascii_to_char(ascii_value: int) -> str:
6+
"""
7+
Converts an ASCII integer value to its character equivalent.
8+
9+
>>> ascii_to_char(65)
10+
'A'
11+
>>> ascii_to_char(97)
12+
'a'
13+
>>> ascii_to_char(48)
14+
'0'
15+
"""
16+
return chr(ascii_value)
17+
18+
if __name__ == "__main__":
19+
import doctest
20+
21+
doctest.testmod()
22+
23+
input_value = input("Enter an ASCII value to convert to a character: ").strip()
24+
try:
25+
ascii_val = int(input_value)
26+
if 0 <= ascii_val <= 127:
27+
print(f"The character for ASCII value {ascii_val} is: {ascii_to_char(ascii_val)}")
28+
else:
29+
print("Please enter a valid ASCII value (0-127).")
30+
except ValueError:
31+
print("Invalid input. Please enter an integer.")

0 commit comments

Comments
 (0)