使用Ascw和Chr的Visual Basic任务

时间:2014-04-20 11:44:31

标签: vb.net ascii

我的代码在这里: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

我的程序需要用逗号分隔数字,将它们改为字母表(例如a = 1,b = 2和c = 3)并打印出结果。 目前,当我进入" e"那么12(是的,我明白了加密函数,但是我在if语句的错误一边编程。),我得到55的输出。我的输出应该是" l"。 我遇到的另一个问题是,如果我使用Chr()将数字转换为ASCII(我用作我的数据库的字母),我会被告知要使用AscW(),而且不会似乎完成了这项工作。 如果任何人有任何解决方案或甚至方法使代码更好,不会影响我的问题,我将非常感激。 谢谢

1 个答案:

答案 0 :(得分:0)

如果您使用的是VB .Net,请查看此代码。

    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)