Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an alphanumeric phone number to digits

UPDATE:

The final version of my utility looks like this:

StringBuilder b = new StringBuilder();

for(char c : inLetters.toLowerCase().toCharArray())
{
    switch(c)
    {
    case '0':                                          b.append("0"); break;
    case '1':                                          b.append("1"); break;
    case '2': case 'a': case 'b': case 'c':            b.append("2"); break;
    case '3': case 'd': case 'e': case 'f':            b.append("3"); break;
    case '4': case 'g': case 'h': case 'i':            b.append("4"); break;
    case '5': case 'j': case 'k': case 'l':            b.append("5"); break;
    case '6': case 'm': case 'n': case 'o':            b.append("6"); break;
    case '7': case 'p': case 'q': case 'r': case 's':  b.append("7"); break;
    case '8': case 't': case 'u': case 'v':            b.append("8"); break;
    case '9': case 'w': case 'x': case 'y': case 'z':  b.append("9"); break;
    }
}

return builder.toString();

ORIGINAL QUESTION:

I'm taking on the simple task of converting an alphanumeric phone number to a string of digits. For example, 1-800-HI-HAXOR would become 1-800-44-42967. My initial attempt was to create a nasty switch statement, but I'd love a more elegant, and efficient solution. Here's what I've got:

for(char c : inLetters.toLowerCase().toCharArray())
{
    switch(c)
    {
    case '0':                                         result+="0"; break;
    case '1':                                         result+="1"; break;
    case '2': case 'a': case 'b': case 'c':           result+="2"; break;
    case '3': case 'd': case 'e': case 'f':           result+="3"; break;
    case '4': case 'g': case 'h': case 'i':           result+="4"; break;
    case '5': case 'j': case 'k': case 'l':           result+="5"; break;
    case '6': case 'm': case 'n': case 'o':           result+="6"; break;
    case '7': case 'p': case 'q': case 'r': case 's': result+="7"; break;
    case '8': case 't': case 'u': case 'v':           result+="8"; break;
    case '9': case 'w': case 'x': case 'y': case 'z': result+="9"; break;
    }
}

Thanks!

like image 358
Reed Olsen Avatar asked Sep 22 '09 21:09

Reed Olsen


People also ask

What is an alphanumeric phone number?

Phonewords are mnemonic phrases represented as alphanumeric equivalents of a telephone number. In many countries, the digits on the telephone keypad also have letters assigned.

How do you decode a phone number?

What do the numbers in a phone number denote? The first three digits are the area code, which is unique to each region. The next three digits also pertains to region, but narrows it down a bit. The remaining digits make up the line number.

How do you convert numbers to alphanumeric?

To convert alpha words or phrases back to numbers, begin by entering the word, phrase or combination of numbers and alpha characters. Click on Convert To Numeric and the number representation will be returned. You may click on Clear Values to try again.

How do you convert letters to numbers?

The Letter-to-Number Cipher (or Number-to-Letter Cipher or numbered alphabet) consists in replacing each letter by its position in the alphabet , for example A=1, B=2, Z=26, hence its over name A1Z26 .

How to convert phone number to digits in AutoCAD?

Select the list with phone numbers you need to convert to digits, then click Kutools > Text > Remove Characters . 2. In the Remove Characters dialog box, please check the Custom box, enter a hyphen – into the text box, and finally click the OK button.

What is the formula to convert a phone number to digits?

=--SUBSTITUTE (SUBSTITUTE (SUBSTITUTE (SUBSTITUTE (A2," (",""),")","")," ",""),"-","") Note: In the formula, A2 is the cell contains the phone number you need to convert to digits.

What is the fastest way to convert a 7 digit number?

However, when converting a 7 digit number, for example 266-7883, you will find it considerably faster to convert 266-0000 first, and then convert 000-7883 after that. Enter zeros in the 3 or 4 digit area that you are NOT converting. The time involved for each number's alpha search increases exponentially.

What is the best way to represent numbers?

Try using signed digits to represent your numbers. For example, instead of using digits 0..9 for decimal numbers, use digits -5..5. This Wikipedia article gives an example for the binary representation of numbers, but the approach can be used for any numeric base.


1 Answers

The switch statement is not really that bad. Your algorithm is linear with respect to the length of the phone number. The code is readable and pretty easy to verify by inspection. I wouldn't mess with it, except to add a default case for handling errors. (I'm not a Java programmer, so forgive me if it's called something else.)

If you have to make it faster, a pre-initialized table indexed by character would avoid any comparisons beyond basic error checking. You could even avoid the case conversion by duplicating the values in the table (digit['A'] = digit['a'] = "2";). The cost of initializing the table would be amortized over the total number of conversions.

like image 98
Adrian McCarthy Avatar answered Oct 13 '22 00:10

Adrian McCarthy