File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ Convert an ASCII integer to its corresponding character.
3+
4+ Example:
5+ >>> ascii_to_char(65)
6+ 'A'
7+ >>> ascii_to_char(97)
8+ 'a'
9+ >>> ascii_to_char(36)
10+ '$'
11+ >>> ascii_to_char(256)
12+ Traceback (most recent call last):
13+ ...
14+ ValueError: ASCII code must be between 0 and 255
15+
16+ References:
17+ https://en.wikipedia.org/wiki/ASCII
18+ """
19+
20+ def ascii_to_char (ascii_value : int ) -> str :
21+ """
22+ Convert an ASCII integer (0-255) into its corresponding character.
23+
24+ Args:
25+ ascii_value (int): Integer representing an ASCII code (0-255).
26+
27+ Returns:
28+ str: The corresponding character for the ASCII value.
29+
30+ Raises:
31+ ValueError: If the input is not within 0-255 inclusive.
32+
33+ >>> ascii_to_char(65)
34+ 'A'
35+ >>> ascii_to_char(128)
36+ '\x80 '
37+ >>> ascii_to_char(300)
38+ Traceback (most recent call last):
39+ ...
40+ ValueError: ASCII value must be in the range 0-255.
41+ """
42+
43+ if not isinstance (ascii_value , int ):
44+ raise TypeError ("Input must be an integer." )
45+ if not (0 <= ascii_value <= 255 ):
46+ raise ValueError ("ASCII code must be between 0 and 255" )
47+ return chr (ascii_value )
You can’t perform that action at this time.
0 commit comments