loop order (again)

ARC

Well-known member
Joined
Sep 9, 2006
Messages
63
Location
Minnesota
Programming Experience
Beginner
I'm trying to get the 'space-less' messege, one letter at a time, and convert that letter into a number between 1 and 26. I'm doing so by turning the letter into ascii value, then minus 96. a = 97 so 97 - 96 = 1. a is the first letter of the alphabet. Then, i use that number (1) in cunjunction with the same process to the keyword letter, to pull a number out of an array i have created in the formload. I then take that pulled number and CHR() it to give me the codedChar, then display it in the uiOutput text box.

The problem im having is with my loop. It's not looping past the first letter for either the keyword, or the messege... but it does fire the correct number of times according to mLength.

I know that this isnt controling the To.Lower yet, but for now i'm just making sure i enter the keyword and messege in all lowercase. I know how to do that, so that's not what i'm worried about yet.

Any suggestions would be great.

EDIT:: the only thing I can think of, is it's some problem with the math being outside of the loop. So i'm gunna try that next, by bringing what i can inside.

VB.NET:
Dim messege As String = Me.uiInput.Text.Replace(" ", "")
        Dim keyWord As String = Me.uiKeyWord.Text

        Dim mLength As Integer = Len(messege)
        Dim kLength As Integer = Len(keyWord)

        Dim mCounter As Integer = 1
        Dim kCounter As Integer = 1

        Dim mTempLetter As String = Microsoft.VisualBasic.Left(messege, mCounter)
        Dim kTempLetter As String = Microsoft.VisualBasic.Left(keyWord, kCounter)

        Dim mASC As Integer = (Asc(mTempLetter) - (96))

        Dim kASC As Integer = (Asc(kTempLetter) - (96))

        Dim codedChar As String


        Me.uiOutput.Text = ""


        Do While mCounter <= mLength

            codedChar = Chr(aGrid(kASC, mASC))

            Me.uiOutput.AppendText(codedChar)

            mCounter += 1
            kCounter += 1


        Loop


This didnt work either::

VB.NET:
Dim mLength As Integer
        Dim kLength As Integer
        Dim mTempLetter As String
        Dim kTempLetter As String

        Dim mCounter As Integer
        Dim kCounter As Integer

        Dim ascK As Integer
        Dim ascM As Integer

        Dim kTemp As Integer
        Dim mTemp As Integer

        Dim codedInt As Integer
        Dim codedChar As String


        mLength = Len(Me.uiInput.Text)
        kLength = Len(Me.uiKeyWord.Text)

        mCounter = 1
        kCounter = 1


        For mCounter = 1 To mLength

            mTempLetter = Microsoft.VisualBasic.Left(Me.uiInput.Text, mCounter)
            kTempLetter = Microsoft.VisualBasic.Left(Me.uiKeyWord.Text, kCounter)

            ascM = Asc(mTempLetter)
            ascK = Asc(kTempLetter)

            mTemp = ((ascM) - (96))
            kTemp = ((ascK) - (96))

            codedInt = aGrid(mTemp, kTemp)
            codedChar = Chr(codedInt)

            Me.uiOutput.AppendText(codedChar)

            kCounter += 1

        Next mCounter
 
Last edited:
says

Type 'StringBuilder' is not defined.

and what's the resTextBox for?

::EDIT::
ok sorry cJard that im not as fluent as you are, but i dont understand the above bits that you posted. If there is a more efficiant way to go about this then what I was doing, that's awesome... and I would have asked for that after I got it all working my way. Partially because im still new to this and was trying to get Do While loops down. Anyways, sorry if I offended you in some way but I'm just doin the best i can.

::EDIT AGAIN::
and the 'math' for decrypting this cycpher is what i am using. The stackCounter is the row #, or basicaly the y value of aGrid, which is what we're looking for.
 
Last edited:
says

Type 'StringBuilder' is not defined.
Point to the wiggly line, see the error correction options. Press F2, search StringBuilder
etc...
Youll need to Imports System.Text at the top of the code. Maybe when you copied the code, you misssed that line?

Always when you build strings, use a stringbuilder

and what's the resTextBox for?
Result textbox. Result of enc/decryption


ok sorry cJard that im not as fluent as you are, but i dont understand the above bits that you posted.
Which bits?
Heres the code again with comments:
VB.NET:
'nneded for stringbuilder
Imports System.Text


Public Class Form1

'maps characters to numbers "a" -> 0, "b" -> 1 etc
    Private numOf As New Dictionary(Of Char, Int32)

'opposite of above
    Private chrOf As New Dictionary(Of Int32, Char)

'holds the key text
    Private keyStr As String



'fires when we type in the encode textbox.
    Private Sub encTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles encTextBox.TextChanged
        MakeKey(encTextBox.Text)

'clear the result textbox
        resTextBox.Clear()

'get the string to encode, no spaces, lowercase cause all my chars are lowercase
        Dim enc As String = encTextBox.Text.ToLower().Replace(" ", "")

'for every char in the string to encode
        For i As Integer = 0 To enc.Length - 1

'add to the result text box, the char of( the number of encode + the number of key, modded by 26)
'expanding this, and reading the wikipedia article
'we can see that vignere is mathematical:
'encoded char = (decoded char + key char) mod 26
'mod is an op that gives the remainder of a divide
'1 mod 26 = 1, 25 mod 26 = 25, 26 mod 26 = 0, 27 mod 26 = 1
'so mod X always gives us a number between 0 and X-1

'in wiki, we have ATTACKATDAWN as the message, LEMON as the key
'so we take A in [B]A[/B]TTACKATDAWN and numOf() it.. it becomes 0
'now we take L in [B]L[/B]EMON and it becomes.. er 13 or something
' now we do 0 + 13 = 13
'now we do 13 mod 26 = 13 
'  (mod is handy, suppose we had 25 + 25 = 50, out of range of the alphabet,
'   mod brings it back in range 50 mod 26 = 24)
'now we find the char of 13 -> L
'now we append L to the result box, and start with the next char
            resTextBox.AppendText(chrOf((numOf(enc(i)) + numOf(keyStr(i))) Mod 26))
        Next

    End Sub

'like above, but for decoding. only interesting bit int he decode is 
'because it requires  a MINUS op we might get neg numbers, and any neg number
'when modded is a neg result.. we dont want that cause its out of range
'so we add 26 first. it doesnt matter if we add 26, or 260 or any multiple of
'26 because MOD will always bring it back in range 0 to 25
    Private Sub decTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles decTextBox.TextChanged
        MakeKey(decTextBox.Text)

        resTextBox.Clear()

        Dim dec As String = decTextBox.Text.ToLower().Replace(" ", "")
        For i As Integer = 0 To dec.Length - 1
            resTextBox.AppendText(chrOf(([B]26 +[/B] (numOf(dec(i)) - numOf(keyStr(i)))) Mod 26))
        Next
    End Sub

'stuff to do on form load. same as subscribing to ones own Form_Load event
    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)

'its the alphabet
        Dim alphabet As String = "abcdefghijklmnopqrstuvwxyz"

'this loop sets up the mappings.. a = 0, b = 1, c = 2 and back again
        For i As Integer = 0 To alphabet.Length - 1
            numOf.Add(alphabet(i), i)
            chrOf.Add(i, alphabet(i))
        Next
    End Sub

'the key must be remade every time the encrypt text changes
'if the enc text is 100 long and the key is 5
'then the key must be repeated 20 times, thats ALL this does
    Private Sub MakeKey(ByVal forText As String)
        If keyTextBox.Text.Length = 0 Then Exit Sub

        Dim keySB As New StringBuilder(keyTextBox.Text)
        While keySB.Length < forText.Length
            keySB.Append(keyTextBox.Text)
        End While
        keySB.Length = forText.Length

        keyStr = keySB.ToString().ToLower()
    End Sub
End Class


[quote
If there is a more efficiant way to go about this [/quote]
Its not really about efficiency, its about writing clean logical code that you dont get confused by or lost in.
Your current code is okay, it essentially does the same as mine, but its dogged by problems that you have patched and patched and patched again

You end up with a working effort that is a pile of patches and its a bit of a mess. I dont mean this to bash your coding.. i mean as a constructive cristicism that there is no subsitute when youre starting out, for weriting plain english comments first and then translating them into code. Pencil and paper first. Brain first. Comments first..

Weeks of coding can save you HOURS of planning :)


was trying to get Do While loops down
For loops are easier for this. We dont do character by character examination of strings by reverse splitting them into single characters, whacking them on a stack and then plater popping them off
A string is an array of characters

myString(4) gets you the fifth character, thats all there is to it

. Anyways, sorry if I offended you in some way but I'm just doin the best i can.
I dont get offended, ever, by anything :) Dont worry :)


and the 'math' for decrypting this cycpher is what i am using. The stackCounter is the row #, or basicaly the y value of aGrid, which is what we're looking for.

Readthe wikipedia article.. yes it might help your human understanding to generate a tabula recta, but the way the machine will work on it is far far easier in just the straight math.

EncI = (KeyI + DecI) Mod 26

and vice versa
 
Perhaps im missing something still... it all seems to make ligical sense i guess, except for your three text boxes... There's the decTextBox/encTextBox/recTextbox

Which is for the user to type in the regular messege (to encode), which is for typing in the coded messege (to decode) and which is for the user to type in the keyword itself?

I have three text boxes on my form, but i cant seem to line up the three you have with my three... or figure out how to decode the messege back to the original text.

:-/ sorry if im missing something obvious... I'll keep looking!
 
sorry you need 4:

keyTextBox = the user types here the key they will use. wikipedia LEMON
encTextBox - the user types here the message they will encode e.g. wiki ATTACKATDAWN
decTextBox = here the user can type an encoded message to be decoded on wiki its something like LFOPRVFRLL or something
resTextBox = when the user types ATTACKATDAWN in the enc text box, the encoded result LFOPRVFRLL will appear here. OR. When the user types LFOPRVFRLL in the decTextBox, the decoded result ATTACKATDAWN will appear here

key -> key
enc -> to be encoded
dec -> to be decoded
res -> result

only type in 2 boxes at a time, either key + enc or key + dec. res
has read only purpose!
 
Back
Top