Results 1 to 5 of 5

Thread: Bug in encHex and decHex!

  1. #1

    Thread Starter
    Hyperactive Member vbzero's Avatar
    Join Date
    Aug 2000
    Location
    Vienna
    Posts
    347

    Red face

    I use those functions to encode and decode a string to hex.

    There is a problem - maybe a little bug - when I encode
    a string 1f;1 then a hex number 12 31 66 3B 2 1F 2 31 will be generated.

    When I decode 12 31 66 3B 2 1F 2 31 then a string 1f;
    will be generated. The function decodes a  instead of 1 in
    the string.

    I can't find that nasty bug! - Can anyone help me, please!?

    Code:
    Public Function encHex(strValue As String) As String
    On Error Resume Next
    Dim i As Integer
    For i = 1 To Len(strValue)
    encHex = encHex + Hex(Asc(Mid(strValue, i, 1)))
    If (Len(encHex) Mod 2) = 0 Or (Len(encHex) - 1 Mod 2) Then
    encHex = encHex + " "
    End If
    Next i
    encHex = Trim(encHex)
    End Function
    
    Public Function decHex(strValue As String) As String
    On Error Resume Next
    Dim sTemp As String
    sTemp = strValue + " "
    Dim i As Integer
    For i = 1 To Len(sTemp) Step 3
    decHex = decHex + Chr(CLng(Val("&h" + Mid(sTemp, i, 2))))
    Next i
    End Function
    Any suggestions?

    thx, vbzero

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    In decHex - use Split() rather than the loop to extract each individual part. Either that, or loop through, checking for spaces.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Hyperactive Member vbzero's Avatar
    Join Date
    Aug 2000
    Location
    Vienna
    Posts
    347

    Question

    Can you please show me that in code?

    thx, vbzero

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    Private Function encHex(sValue As String) As String
        On Error Resume Next
        Dim i As Integer
        For i = 1 To Len(sValue)
            encHex = encHex + Hex(Asc(Mid(sValue, i, 1))) + " "
        Next i
        encHex = Trim(encHex)
    End Function
    Private Function decHex(sValue As String) As String
        On Error Resume Next
        Dim sTemp As String
        Dim sCh As String
        Dim i As Integer
    
        For i = 1 To Len(sValue)
            sCh = Mid(sValue, i, 1)
            If sCh = " " Then
                decHex = decHex + Chr(CInt(Val("&h" + sTemp)))
                sTemp = ""
            Else
                sTemp = sTemp + sCh
            End If
        Next i
        decHex = decHex + Chr(CInt(Val("&h" + sTemp)))
    End Function
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Hyperactive Member vbzero's Avatar
    Join Date
    Aug 2000
    Location
    Vienna
    Posts
    347

    Thumbs up

    Thank you very much! This problem is solved!

    thx, vbzero

Posting Permissions

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



Click Here to Expand Forum to Full Width