在为变量赋值之前已使用变量

时间:2014-07-29 17:31:39

标签: vb.net

我无法在任何地方找到我的具体问题的答案,所以我想我会打开一个新问题。

我有一个程序可以在ASCII和Binary之间转换文本。它通过在第一个数组中查找输入,获取该输入的索引并在第二个数组中查看该索引号,然后将其找到的内容写入另一个变量来实现此目的。

以下是代码:

Function ConvertBinaryToASCII(ByVal input As String) As String
    Dim ASCIIList() As String = {" ", "!", "a", "A", "b", "B", "c", "C", "d", "D", "e", "E", "f", "F", "g", "G", "h", "H", "i", "I", "j", "J", "k", "K", "l", "L", "m", "M", "n", "N", "o", "O", "p", "P", "q", "Q", "r", "R", "s", "S", "t", "T", "u", "U", "v", "V", "w", "W", "x", "X", "y", "Y", "z", "Z"}
    Dim BinaryList() As String = {"00100000", "00100001", "01100001", "01000001", "01100010", "01000010", "01100011", "01000011", "01100100", "01000100", "01100101", "01000101", "01100110", "01000110", "01100111", "01000111", "01101000", "01001000", "01101001", "01001001", "01101010", "01001010", "01101011", "01001011", "01101100", "01001100", "01101101", "01001101", "01101110", "01001110", "01101111", "01001111", "01110000", "01010000", "01110001", "01010001", "01110010", "01010010", "01110011", "01010011", "01110100", "01010100", "01110101", "01010101", "01110110", "01010110", "01110111", "01010111", "01111000", "01011000", "01111001", "01011001", "01111010", "01011010"}
    Dim BinarySubstrings = Enumerable.Range(0, input.Length \ 8).[Select](Function(i) input.Substring(i * 8, 8)).ToArray()
    Dim counter As Integer = 0
    Dim result() As String
    Dim binaryMatch As Integer

    For Each e As String In BinarySubstrings
        binaryMatch = Array.IndexOf(BinaryList, e)
        result(counter) = ASCIIList(CInt(binaryMatch))
        counter += 1
    Next

    Return String.Join("", result)
End Function

该功能按预期工作,但问题在于无论我如何调用或初始化result,它总会给我一些错误,我无法弄清楚如何使用它。< / p>

3 个答案:

答案 0 :(得分:3)

使用列表(字符串)更好,因为您不需要知道数组的大小。
当然,您需要在使用前初始化List(Of String)。

Function ConvertBinaryToASCII(ByVal input As String) As String
    Dim ASCIIList() As String = .....
    Dim BinaryList() As String = ......
    Dim BinarySubstrings = Enumerable.Range(0, input.Length \ 8).[Select](Function(i) input.Substring(i * 8, 8)).ToArray()
    Dim result = new List(Of String)()
    Dim binaryMatch As Integer

    For Each e As String In BinarySubstrings
        binaryMatch = Array.IndexOf(BinaryList, e)
        result.Add(ASCIIList(CInt(binaryMatch)))
    Next

    Return String.Join("", result.ToArray())
End Function

答案 1 :(得分:2)

您已声明了一个数组result,但该变量尚未分配一个数组实例,因此它为空。您无法将对象添加到空数组。也许你的意思是:

Dim result(BinarySubstrings.Length) As String

如果您不确定结果的大小,请考虑使用评论中提到的List(Of String)

答案 2 :(得分:2)

有一种更简单的方法:

Function BinaryStringToAscii(s As String) As String
    If s.Length Mod 8 <> 0 Then
        Throw New ArgumentException("String length is not a multiple of 8.")
    End If

    Dim sb As New Text.StringBuilder

    For i = 0 To s.Length - 1 Step 8
        Dim bin = s.Substring(i, 8)
        sb.Append(Chr(Convert.ToInt32(bin, 2)))
    Next

    Return sb.ToString()

End Function

它使用StringBuilder,因为最终你需要一个字符串,而不需要使用数组或列表。 Convert.ToInt32函数有一个有用的重载,您可以指定从中转换字符串的基数(2,8,10或16)。

反过来说:

Function AsciiToBinaryString(s As String) As String
    Dim sb As New Text.StringBuilder
    For Each c In s
        sb.Append(Convert.ToString(Asc(c), 2).PadLeft(8, "0"c))
    Next

    Return sb.ToString()

End Function