Domanda

My code is here: http://pastebin.com/raw.php?i=cQrMWmuC

Module Program
    Sub Main()
        'startup procedure
        Console.WriteLine("Please enter what you wish to do. Encrypt(enter E) or Decrypt(enter D)")
        Dim choice As String
        choice = Console.ReadLine



        'universal variables    
        Dim input As String             'both methods can use this. just a standard input variable. May change if decryptor decides to use string input

        'variables for encryptor
        Dim length As Int16  'this is int16 because operator does not work for char value types
        Dim word As String
        Dim eoutput As String 'eoutput means encryptor output

        'variables for decryptor



        If choice = "e" Or choice = "E" Then


            input = Console.ReadLine
            length = Len(input)
            Dim value1 As Int16
            Dim value2 As Int16
            For c=1 To length Step 2
                value1 = Mid(input, c,2)
                If Mid(word, c+1,1)="," Then
                    input=Mid(word, c,1)
                End If
                If Mid (word,c,1) = "," Then
                    input=Mid(word, c+1,1)
                End If
                If input = mid(word, c, 2) Then 
                    c=c+1
                End If
                value2 = value1 + 64 'and later the key
                eoutput = AscW(value2)

                Console.WriteLine(eoutput)
            Next





        Else
            'Enter Decrption tools
            ' TODO: Implement Functionality Here
        End If



        Console.Write("Press any key to continue . . . ")
        Console.ReadKey(True)
    End Sub
End Module

My program needs to take in numbers separated by commas, change them to the alphabet(e.g. a = 1, b = 2 and c = 3) and print out the result. Currently, when I enter "e" then 12 (yes I understand that's the encrypt function but I am programming in the wrong side of the if statement.), I get an output of 55. My output should be "l". The other problem I'm having is that if I use Chr() to convert from number to ASCII (what i'm using as my database for my letters) I get told to use AscW(), and that doesn't seem to do the job. If anyone has any solutions or even ways to make the code better that doesn't affect my problem, I would appreciate it greatly. Thanks

È stato utile?

Soluzione

If you are using VB .Net then take a look at this code.

    Dim sampleUserInput As String = "8, 5, 12,12, 15, a,99,52" 'with errors
    Dim theLetters As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    '
    'step 1 separate the user input
    Dim seps() As Char = New Char() {","c, " "c}
    Dim nums() As String = sampleUserInput.Split(seps, StringSplitOptions.RemoveEmptyEntries)

    'step 2 look at each number and use it
    'to get the letter
    Dim cnvrtd As New System.Text.StringBuilder
    Dim idx As Integer
    For Each s As String In nums 'look at each number
        'convert string to number and check that it is in range
        If Integer.TryParse(s, idx) AndAlso idx <= theLetters.Length Then
            cnvrtd.Append(theLetters(idx - 1)) 'use the number to get a character
        End If
    Next
    Debug.WriteLine(cnvrtd.ToString)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top