Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Letters to numbers

Status
Not open for further replies.

TrollBro

Technical User
Sep 4, 2004
98
US
I'm trying to find an efficient way to convert a string value, such as "ABCDzF45" to a nuneric code "123426645" where a=1, b=2, c=3, C=3, etc, and n=n. (does not have to be case sensitive but ok if it is).

Does anyone know if there is a fast/efficient way to do this?

Thanks!
 
Hi,
Code:
Function Str2Digits(str As String) As String
   For i = 1 To Len(str)
      s = Mid(UCase(str), i, 1)
      Select Case s
         Case "A" To "Z"
            Str2Digits = Str2Digits & Chr(Asc(s) - 16)
         Case "0" To "9"
            Str2Digits = Str2Digits & s
      End Select
   Next
End Function


Skip,

[glasses] [red]Be advised:[/red] It's been reported that a wee psychic is roaming the countryside.
Small Medium @ Large! [tongue]
 

Thanks Skip
Works extremely efficiently, but it only produces numbers for letters A through I, then J through P generate various characters before finishing with Q though Z with CAP letters A throgh J.

Any way to keep the results all numeric that you can think of? A=1, Z=26?

Thanks!
 
My first solution rather convoluted, huh?
Code:
         Case "A" To "Z"
            Str2Digits = Str2Digits & Asc(s) - 64

Skip,

[glasses] [red]Be advised:[/red] It's been reported that a wee psychic is roaming the countryside.
Small Medium @ Large! [tongue]
 

Skip

not so convoluted - I just couldn't figure out the -16.
Works perfect with the -64, but having seen the -16 results limitless possibilities for my algorithm have been revealed.

Thanks!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top