乐闻世界logo
搜索文章和话题

How to convert numeric characters in ASCII

2026年2月21日 16:19

ASCII values of numeric characters 0-9 and conversion methods:

ASCII Values of Numeric Characters:

  • '0': 48
  • '1': 49
  • '2': 50
  • '3': 51
  • '4': 52
  • '5': 53
  • '6': 54
  • '7': 55
  • '8': 56
  • '9': 57

Conversion Methods:

1. Numeric Character to Integer:

python
char = '5' num = ord(char) - ord('0') # 5 # or num = ord(char) - 48 # 5

2. Integer to Numeric Character:

python
num = 5 char = chr(num + ord('0')) # '5' # or char = chr(num + 48) # '5'

3. Check if Character is a Digit:

python
char = '5' is_digit = '0' <= char <= '9' # True # or is_digit = 48 <= ord(char) <= 57 # True

4. Batch Conversion Examples:

python
# String to Integer num_str = "123" num = 0 for c in num_str: num = num * 10 + (ord(c) - ord('0')) # num = 123 # Integer to String num = 123 if num == 0: num_str = "0" else: num_str = "" while num > 0: num_str = chr(num % 10 + ord('0')) + num_str num //= 10 # num_str = "123"

Important Notes:

  • ASCII values of numeric characters are consecutive
  • Verify character validity before conversion
  • Additional logic needed for handling negative numbers
标签:ASCII