Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions conversions/ascii_to_char.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
ascii_to_char.py
----------------
Converts an ASCII value (integer) to its corresponding character.

Example:
>>> ascii_to_char(65)
'A'
>>> ascii_to_char(97)
'a'
"""


def ascii_to_char(ascii_value: int) -> str:
"""
Convert an ASCII value to its corresponding character.
Raises ValueError if the ASCII value is not valid.
"""
if not (0 <= ascii_value <= 127):
raise ValueError("ASCII value must be between 0 and 127.")
return chr(ascii_value)


if __name__ == "__main__":
import doctest

doctest.testmod()