Link to home
Start Free TrialLog in
Avatar of Akkas
Akkas

asked on

String Characters

Team,

I am trying to devise some code that will enable me to do some simple encoding and decoding.

I have a text box and when the user enters Characters, words or number's I would like to allow the user to click an encode button so that 'each' character is encoded (ASCII Code - 1) and is displayed in the text box as characters.

Then the user is able to hit another command button and Decode the characters back to there original state (Encoded ASCII + 1).

I am finidng it difficult to get each individual character and applying the Encode method on it, any help wold be greatly appreciated.

Thanks in advance,
Akkas

Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Try this:

   Create a form with one textbox (txtValue) and two command buttons (cmdEncode and cmdDecode), then add the following code to it:

Private Sub cmdEncode_Click()

    txtValue.Text = Encode(txtValue.Text, True)

End Sub

Private Sub cmdDecode_Click()

    txtValue.Text = Encode(txtValue.Text, False)

End Sub

Private Function Encode(ByVal pString As String, ByVal pEncodeFlag As Boolean) As String

    Dim sEncoded As String
    Dim iDir As Integer
    Dim i As Integer
   
    If pEncodeFlag = True Then
        iDir = 1
    Else
        iDir = -1
    End If
   
    For i = 1 To Len(pString)
        sEncoded = sEncoded & Chr(Asc(Mid(pString, i, 1)) + iDir)
    Next i
   
    Encode = sEncoded

End Function


Hope this helps.
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Akkas
Akkas

ASKER

Quality!!!! You've done this before, no doubt!!!!

I came up with the code below whilst I was waiting for a repsonse,

Private Sub cmdEncode_Click()
Dim Encoded As Variant
 Word = txtValue.Text
'Add Characters to variable
 For Ascii = 1 To Len(Word)
    Encode = Asc(Mid(Word, Ascii, 1)) - 1
    Encoded = Encoded & Chr(Encode)
  Next Ascii
   txtValue.Text = Encoded
End If
End Sub

You have used a function which requires minimal code for each command button, Im new to VB6.

Thanks and its a great help!
Akkas