You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将字符转换为特定整数?电话键盘字母转号码编程咨询

7-Letter Word to Phone Number Conversion Solution

Hey Carlos, let's tackle this problem together—you've already got the input handling sorted, so we just need to nail the conversion logic and formatting. Here's how to do it step by step:

First, Clarify the Telephone Keypad Mapping

Let's lock in the standard letter-to-number rules clearly:

  • 2 → A, B, C (uppercase or lowercase)
  • 3 → D, E, F
  • 4 → G, H, I
  • 5 → J, K, L
  • 6 → M, N, O
  • 7 → P, Q, R, S
  • 8 → T, U, V
  • 9 → W, X, Y, Z

Numbers 0 and 1 don't map to any letters, so we won't need to handle those for your 7-letter input.

Core Conversion Logic

The key here is using a mapping dictionary to quickly look up the number for each character. Plus, we'll normalize the input to uppercase (or lowercase) so we don't have to handle both cases separately.

Example Code (Python)

Since you didn't specify a language, I'll use Python—it's readable and perfect for this kind of string manipulation:

# Define the letter-to-number mapping
keypad_map = {
    'A': '2', 'B': '2', 'C': '2',
    'D': '3', 'E': '3', 'F': '3',
    'G': '4', 'H': '4', 'I': '4',
    'J': '5', 'K': '5', 'L': '5',
    'M': '6', 'N': '6', 'O': '6',
    'P': '7', 'Q': '7', 'R': '7', 'S': '7',
    'T': '8', 'U': '8', 'V': '8',
    'W': '9', 'X': '9', 'Y': '9', 'Z': '9'
}

def word_to_phone(word):
    # Normalize input to uppercase to handle lowercase letters
    upper_word = word.upper()
    # Convert each character to its corresponding number
    digits = [keypad_map[char] for char in upper_word]
    # Combine digits into a string, then split into 3-4 format (matches your example)
    phone_number = f"{''.join(digits[:3])}-{''.join(digits[3:])}"
    return phone_number

# Test with your example: PAINTER should return 724-6837
print(word_to_phone("PAINTER"))  # Output: 724-6837
print(word_to_phone("painter"))  # Output: 724-6837 (works with lowercase too)

Converting Characters to Integers

If you specifically need integer values instead of strings (though phone numbers are usually treated as strings to preserve formatting), you can adjust the code slightly:

  • Store integers directly in the mapping: 'A': 2 instead of 'A': '2'
  • Or convert the string digits to integers after lookup: digits = [int(keypad_map[char]) for char in upper_word]

Handling Full Phone Numbers (Like 1-800-PAINTER)

If you need to handle full numbers with prefixes, you can split the input, convert the letter segment, then recombine:

def full_number_conversion(full_input):
    # Split input into parts (e.g., "1-800-PAINTER" → ["1", "800", "PAINTER"])
    parts = full_input.split('-')
    # Convert the letter part using our existing function
    converted_part = word_to_phone(parts[-1])
    # Recombine with the prefix segments
    return '-'.join(parts[:-1] + [converted_part])

print(full_number_conversion("1-800-PAINTER"))  # Output: 1-800-724-6837

Key Tips

  • Normalize Case: Using .upper() ensures you don't need duplicate entries for lowercase letters in your mapping.
  • Dictionary Lookup: This is way cleaner and faster than writing a bunch of if/elif statements.
  • String Formatting: F-strings make it easy to split the 7-digit result into the standard 3-4 segment format.

Let me know if you need help adapting this to a specific language, or if you run into edge cases like non-letter characters!

内容的提问来源于stack exchange,提问作者Carlos Valeriano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:19:18