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

AkkasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
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.
0
Carl TawnSystems and Integration DeveloperCommented:
Ooops, just reread the question, change:

        If pEncodeFlag = True Then
            iDir = 1
        Else
            iDir = -1
        End If

To:

        If pEncodeFlag = True Then
            iDir = -1
        Else
            iDir = 1
        End If
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AkkasAuthor Commented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.