Skip to content

Commit 878e26d

Browse files
committed
Add ascii_to_char conversion script (Fixes #13569)
1 parent e2a78d4 commit 878e26d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

conversions/ascii_to_char.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
ascii_to_char.py
3+
----------------
4+
Converts an ASCII value (integer) to its corresponding character.
5+
6+
Example:
7+
>>> ascii_to_char(65)
8+
'A'
9+
>>> ascii_to_char(97)
10+
'a'
11+
"""
12+
13+
def ascii_to_char(ascii_value: int) -> str:
14+
"""
15+
Convert an ASCII value to its corresponding character.
16+
Raises ValueError if the ASCII value is not valid.
17+
"""
18+
if not (0 <= ascii_value <= 127):
19+
raise ValueError("ASCII value must be between 0 and 127.")
20+
return chr(ascii_value)
21+
22+
23+
if __name__ == "__main__":
24+
import doctest
25+
doctest.testmod()

0 commit comments

Comments
 (0)