Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2003
    Posts
    1

    Unanswered: 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 03:04.

  2. #2
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •