If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Delphi, C etc > convert a String of letters to an Integer number

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-02-03, 06:27
athjim athjim is offline
Registered User
 
Join Date: Jan 2003
Posts: 1
convert a String of letters to an Integer number

Hello, I want some help.
I would like a program in C language by preference, how can convert a String of letters (or some words analyzed to letters) to an Integer number.
Each letter has a value based on the following formula: a=599, b=120, c=357, d=409, e=516, f=696,…,z=276
At the end of the conversion all the values of each letter must be added to a total (number).

For example the string : bad = 120 + 599 + 409 =
So is the total number.

Thanks a lot, bye.


Last edited by athjim; 01-03-03 at 02:04.
Reply With Quote
  #2 (permalink)  
Old 01-02-03, 10:13
Bruce A. Baasch Bruce A. Baasch is offline
Registered User
 
Join Date: Nov 2002
Location: Ohio
Posts: 90
Here's a VB function that should work for you:

Private Function StringToNumber(tmpString as String) as Integer
Dim tmpIndex as Integer
'
' clear results if no string
'
StringToNumber = 0
If Len(tmpString) < 1 Then
Exit Function
End If
'
' Loop through string byte by byte
'
For tmpIndex = 1 to Len(tmpString)
StringToNumber = StringToNumber _
+ Asc(Ucase(Mid$(tmpString,tmpIndex,1))) - 64
Next tmpIndex

End Function

64 is subtracted from the Ascii value of each byte "A" = 65 - 64 = 1,
"Z" = 90 - 64 = 26. This routine only works for letters. If you have punctuation or spaces, remove is from tmpString before calling this function.

Good Luck,
__________________
Bruce Baasch
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On