Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何使用自定义表将字符串转换为字节数组,以便将要转换的字母转换为要由BinaryWriter写入的字节?_Vb.net - Fatal编程技术网

Vb.net 如何使用自定义表将字符串转换为字节数组,以便将要转换的字母转换为要由BinaryWriter写入的字节?

Vb.net 如何使用自定义表将字符串转换为字节数组,以便将要转换的字母转换为要由BinaryWriter写入的字节?,vb.net,Vb.net,我不知道如何处理这个问题。下面是一个我的代码的例子,我正在努力。正如你所看到的,我正在以一种乏味的方式编写代码。我只知道如何抓取其中一个字母并将其转换为一个字节。我没有看到过如何在线使用自定义表的示例,该表甚至可以处理字符串的长度。只有使用公共编码的代码。如果有人能给我一个更简单的方法,我将衷心感谢 Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenu

我不知道如何处理这个问题。下面是一个我的代码的例子,我正在努力。正如你所看到的,我正在以一种乏味的方式编写代码。我只知道如何抓取其中一个字母并将其转换为一个字节。我没有看到过如何在线使用自定义表的示例,该表甚至可以处理字符串的长度。只有使用公共编码的代码。如果有人能给我一个更简单的方法,我将衷心感谢

Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
    Dim fileName As String = OfdOpenFile.FileName
    Dim fileStream As IO.FileStream = New IO.FileStream(fileName, IO.FileMode.Open, IO.FileAccess.Write)
    Dim bw As New BinaryWriter(fileStream)
    fileStream.Position = 2348
    If TxtHeroName.TextLength = 1 Then
        If TxtHeroName.Text = " " Then
            bw.Write(CByte(76))
        End If
        If TxtHeroName.Text = "." Then
            bw.Write(CByte(175))
        End If
        If TxtHeroName.Text = "A" Then
            bw.Write(CByte(186))
        End If
        If TxtHeroName.Text = "B" Then
            bw.Write(CByte(187))
        End If
        If TxtHeroName.Text = "C" Then
            bw.Write(CByte(188))
        End If
        If TxtHeroName.Text = "D" Then
            bw.Write(CByte(189))
        End If
        If TxtHeroName.Text = "E" Then
            bw.Write(CByte(190))
        End If
        If TxtHeroName.Text = "F" Then
            bw.Write(CByte(191))
        End If
        If TxtHeroName.Text = "G" Then
            bw.Write(CByte(192))
        End If
        If TxtHeroName.Text = "H" Then
            bw.Write(CByte(193))
        End If
        If TxtHeroName.Text = "I" Then
            bw.Write(CByte(194))
        End If
        If TxtHeroName.Text = "J" Then
            bw.Write(CByte(195))
        End If
        If TxtHeroName.Text = "K" Then
            bw.Write(CByte(196))
        End If
        If TxtHeroName.Text = "L" Then
            bw.Write(CByte(197))
        End If
        If TxtHeroName.Text = "M" Then
            bw.Write(CByte(198))
        End If
        If TxtHeroName.Text = "N" Then
            bw.Write(CByte(199))
        End If
        If TxtHeroName.Text = "O" Then
            bw.Write(CByte(200))
        End If
        If TxtHeroName.Text = "P" Then
            bw.Write(CByte(201))
        End If
        If TxtHeroName.Text = "Q" Then
            bw.Write(CByte(202))
        End If
        If TxtHeroName.Text = "R" Then
            bw.Write(CByte(203))
        End If
        If TxtHeroName.Text = "S" Then
            bw.Write(CByte(204))
        End If
        If TxtHeroName.Text = "T" Then
            bw.Write(CByte(205))
        End If
        If TxtHeroName.Text = "U" Then
            bw.Write(CByte(206))
        End If
        If TxtHeroName.Text = "V" Then
            bw.Write(CByte(207))
        End If
        If TxtHeroName.Text = "W" Then
            bw.Write(CByte(208))
        End If
        If TxtHeroName.Text = "X" Then
            bw.Write(CByte(209))
        End If
        If TxtHeroName.Text = "Y" Then
            bw.Write(CByte(210))
        End If
        If TxtHeroName.Text = "Z" Then
            bw.Write(CByte(211))
        End If
    End If

End Sub

我认为最合适的方法是创建您自己的类,该类继承
编码
,然后以与使用
ascienceoding
UTF8Encoding
等类完全相同的方式使用该类。我相信您可以查看这些类的.NETFramework源代码,以了解它们是如何实现的,尽管这将是C#而不是VB

在这里,我可以想到一种创建转换表的方法:

'A list of characters as a string.
Const chars As String = "ABC"

'A list of hexadecimal bytes as a space-delimited string.
Const bytes As String = "BA BB BC"

Dim charArray = chars.ToArray()
Dim byteArray = bytes.Split().Select(Function(s) System.Convert.ToByte(s, 16)).ToArray()

Dim bytesByChar = Enumerable.Range(0, charArray.Length).ToDictionary(Function(i) charArray(i), Function(i) byteArray(i))
Dim charsByByte = Enumerable.Range(0, byteArray.Length).ToDictionary(Function(i) byteArray(i), Function(i) charArray(i))
然后,编码将是使用
Char
作为
bytesByChar
的键来获得
字节
,解码将使用
Byte
作为
charsByByte
的键来获得
Char