I found this on a website. It's vb (I think) and it is supposed to take a string consisting of both characters and numbers and return only numbers.

I spent the last hour hacking it up and pretty much destroying it trying to get it to work within vfp9. I'll paste what I've done below the actual code.

Actual code:

Dim Sum As Long = 0Dim Index As Integer = 1For Each ch As Char In lcString If Char.Isdigit(ch) Then Sum += Sum + Integer.Parse(ch) * (Index * 2) ElseIf Char.IsLetter(ch) Then Select Case ch.ToString.ToUpper Case "A" Sum += Sum + 10 * (Index * 2) Case "B" Sum += Sum + 11 * (Index * 2) Case "C" Sum += Sum + 12 * (Index * 2) Case "D" Sum += Sum + 13 * (Index * 2) Case "E" Sum += Sum + 14 * (Index * 2) Case "F" Sum += Sum + 15 * (Index * 2) Case "G" Sum += Sum + 16 * (Index * 2) Case "H" Sum += Sum + 17 * (Index * 2) Case "I" Sum += Sum + 18 * (Index * 2) Case "J" Sum += Sum + 19 * (Index * 2) Case "K" Sum += Sum + 20 * (Index * 2) Case "L" Sum += Sum + 21 * (Index * 2) Case "M" Sum += Sum + 22 * (Index * 2) Case "N" Sum += Sum + 23 * (Index * 2) Case "O" Sum += Sum + 24 * (Index * 2) Case "P" Sum += Sum + 25 * (Index * 2) Case "Q" Sum += Sum + 26 * (Index * 2) Case "R" Sum += Sum + 27 * (Index * 2) Case "S" Sum += Sum + 28 * (Index * 2) Case "T" Sum += Sum + 29 * (Index * 2) Case "U" Sum += Sum + 30 * (Index * 2) Case "V" Sum += Sum + 31 * (Index * 2) Case "W" Sum += Sum + 32 * (Index * 2) Case "X" Sum += Sum + 33 * (Index * 2) Case "Y" Sum += Sum + 34 * (Index * 2) Case "Z" Sum += Sum + 35 * (Index * 2) End Select End If Index += 1 Next Return Sum

FUNCTION char_to_numLPARAMETERS lcStringDim Sum As LongDim Index As IntegerSUM = 0INDEX = 1For Each ch As Char In lcString DO CASE CASE VARTYPE(ch) = "N" Sum = Sum + Integer.Parse(ch) * (Index * 2) CASE VARTYPE(ch) = "C" ch=UPPER(ch) ENDCASE DO CASE Case ch="A" Sum = Sum + 10 * (Index * 2) Case ch="B" Sum = Sum + 11 * (Index * 2) Case ch="C" Sum = Sum + 12 * (Index * 2) Case ch="D" Sum = Sum + 13 * (Index * 2) Case ch="E" Sum = Sum + 14 * (Index * 2) Case ch="F" Sum = Sum + 15 * (Index * 2) Case ch="G" Sum = Sum + 16 * (Index * 2) Case ch="H" Sum = Sum + 17 * (Index * 2) Case ch="I" Sum = Sum + 18 * (Index * 2) Case ch="J" Sum = Sum + 19 * (Index * 2) Case ch="K" Sum = Sum + 20 * (Index * 2) Case ch="L" Sum = Sum + 21 * (Index * 2) Case ch="M" Sum = Sum + 22 * (Index * 2) Case ch="N" Sum = Sum + 23 * (Index * 2) Case ch="O" Sum = Sum + 24 * (Index * 2) Case ch="P" Sum = Sum + 25 * (Index * 2) Case ch="Q" Sum = Sum + 26 * (Index * 2) Case ch="R" Sum = Sum + 27 * (Index * 2) Case ch="S" Sum = Sum + 28 * (Index * 2) Case ch="T" Sum = Sum + 29 * (Index * 2) Case ch="U" Sum = Sum + 30 * (Index * 2) Case ch="V" Sum = Sum + 31 * (Index * 2) Case ch="W" Sum = Sum + 32 * (Index * 2) Case ch="X" Sum = Sum + 33 * (Index * 2) Case ch="Y" Sum = Sum + 34 * (Index * 2) Case ch="Z" Sum = Sum + 35 * (Index * 2) ENDCASE Index = Index + 1 NextENDFOR Return Sum

Dim Sum As LongError in line 42: Unrecognized command verb.Dim Index As IntegerError in line 43: Unrecognized command verb.ENDFORError in line 113: There is a missing keyword in the FOR...ENDFOR or DO CASE...ENDCASE command structure.

Sum = 0Index = 1FOR i = 1 TO LEN(lcString) ch = SUBSTR(lcString, i, 1) IF ISDIGIT(ch) Sum = Sum + VAL(ch)*(Index*2) ELSE IF ISALPHA(ch) SUM = SUM + (ASC(ch)-55)*(Index*2) ENDIF ENDIF Index = Index + 1ENDFORReturn Sum

CaptainCyril that is fantastic. I can't believe the code is so small.
I'm closing this to award you the points.

I do have another question, but I'll post it separately since you did answer my original question. The new post will ask if this can this be reversed to get the original string back.

Your first source of code should be suspicious. Code is never of high quality if lines repat in it. This was a useless arrangement of cases. VB also has the ASC() function.

And if the vfp compiler tells you it doesn't knwo a function or command, well, there is the MSDN reference to search for DIM (via google most probably more successful) and find out it's just like LOCAL a variable definition.

Bye, Olaf.

0

Featured Post

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Microsoft Visual FoxPro (short VFP) is a programming language with it’s own IDE and database, ranking somewhat between Access and VB.NET + SQL Server (Express).
Product Description: http://msdn.microsoft.com/en-us/vfoxpro/default.aspx (http://msd…

Applications for our next round of the Experts Exchange Scholarship Contest are starting to roll in. It made us wonder what our past winners are up to these days. Here's a look at what four winners experienced with the contest and what they're doing…

Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…