Ascii Values Of String

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adnanahmed714
    New Member
    • Aug 2006
    • 20

    Ascii Values Of String

    hi all

    i have a string
    <cr><lf>999,1.5 2,32.85,5.91,15 .81,1.39,26.17, 15.75,12.3
    want to calculate its ASCII values sum which is equal to 2411.How can i calculate ascii sum of all the string values.

    thanks in advance
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by adnanahmed714
    hi all

    i have a string
    <cr><lf>999,1.5 2,32.85,5.91,15 .81,1.39,26.17, 15.75,12.3
    want to calculate its ASCII values sum which is equal to 2411.How can i calculate ascii sum of all the string values.

    thanks in advance
    For a start, how are you counting? I tried it and got either 2367 or 3011, depending on whether I converted the "<cr><lf>" to carriage return (13) and linefeed (10) characters or not. Are you skipping the commas? Do you have trailing blanks that are included in the count?

    Um...

    Ok, I tried adding up the values of the actual numbers (first 999, then 1.52, and so on) and got 1110.7. So I still don't get it.
    If it's any help, here's the routine I wrote to try it out. This was under VB6, and I just created a new project with one form.
    Code:
    Option Explicit
    DefLng A-Z
    Private Sub Form_Click()
      Dim s As String
      Dim TempNum As String, Char As String * 1
      Dim I As Long, T As Long, T2 As Single
      
      's = vbCrLf & "999,1.52,32.85,5.91,15.81,1.39,26.17,15.75,12.3"
      s = "<cr><lf>999,1.52,32.85,5.91,15.81,1.39,26.17,15.75,12.3"
      For I = 1 To Len(s)
        Char = Mid$(s, I, 1)
        T = T + Asc(Char)
        Debug.Print Asc(Char), T
      Next
      Debug.Print "Method 1: "; T
      
      For I = 1 To Len(s)
        Char = Mid$(s, I, 1)
        Select Case Char
          Case "0" To "9", "."
            TempNum = TempNum & Char
          Case Else
            T2 = T2 + Val(TempNum)
            Debug.Print TempNum, T2
            TempNum = ""
        End Select
      Next
      T2 = T2 + Val(TempNum)
      Debug.Print "Method 2: "; T2
    End Sub
    Here's what it displayed...
    Code:
     49            2864 
     50            2914 
     46            2960 
     51            3011 
    Method 1:  3011 
                   0 
                   0 
                   0 
                   0 
                   0 
                   0 
                   0 
                   0 
    999            999 
    1.52           1000.52 
    32.85          1033.37 
    5.91           1039.28 
    15.81          1055.09 
    1.39           1056.48 
    26.17          1082.65 
    15.75          1098.4 
    Method 2:  1110.7

    Comment

    Working...