【问题标题】:Byte > String > Byte > File VB字节 > 字符串 > 字节 > 文件 VB
【发布时间】:2016-09-09 17:40:28
【问题描述】:

我想将字节转换为字符串并返回,我已经尝试过:

Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim bytes() As Byte = My.Computer.FileSystem.ReadAllBytes("C:\Archive.zip")

            Dim filestream As System.IO.FileStream = System.IO.File.Create("C:\Archive2.zip")

            Dim info As Byte() = fromstringtobyte(frombytetostring(bytes))
            filestream.Write(info, 0, info.Length)
            filestream.Close()
        End Sub
        Private Function frombytetostring(ByVal b() As Byte)
            Dim s As String
            s = Convert.ToBase64String(b)
            Return s

        End Function
        Private Function fromstringtobyte(ByVal s As String)
            Dim b() As Byte
            b = System.Text.Encoding.UTF8.GetBytes(s)
            Return b
        End Function
    End Class

创建的新文件已损坏。 您能否推荐其他解决方案?

对不起,我的英语不好,这不是我的主要语言。

【问题讨论】:

    标签: vb.net string byte bytearray


    【解决方案1】:

    您的字节到字符串的转换是错误的。你需要使用:

    System.Text.Encoding.[your encoding].GetString(bytes)
    

    来源:

    How to: Convert an Array of Bytes into a String in Visual Basic

    How to: Convert Strings into an Array of Bytes in Visual Basic


    您可能还想阅读本文来决定使用哪种编码:The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

    【讨论】:

    • 我如何知道使用哪种编码?
    • 您需要在字符串到字节和字节到字符串中使用相同的编码。您选择哪一种取决于各种因素。请参阅我发布的链接。
    • 你发布了什么链接?
    【解决方案2】:

    我找到了答案,新代码是:

    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim bytes() As Byte = My.Computer.FileSystem.ReadAllBytes("C:\Program Files (x86)\Windows Repair Pro\Windows Repair Pro (All In One) 3.8.7\Tweaking.com - Windows Repair Portable\Archive.zip")
    
            Dim filestream As System.IO.FileStream = System.IO.File.Create("C:\Program Files (x86)\Windows Repair Pro\Windows Repair Pro (All In One) 3.8.7\Tweaking.com - Windows Repair Portable\Archive2.zip")
    
            Dim info As Byte() = fromstringtobyte(frombytetostring(bytes))
            filestream.Write(info, 0, info.Length)
            filestream.Close()
        End Sub
        Private Function frombytetostring(ByVal b() As Byte)
            Dim s As String
            s = BitConverter.ToString(b)
            Return s.Replace("-", "")
        End Function
        Private Function fromstringtobyte(ByVal s As String)
            Dim B() As Byte = New Byte(s.Length / 2 - 1) {}
            For i As Integer = 0 To s.Length - 1 Step 2
                B(i / 2) = Convert.ToByte(s.Substring(i, 2), 16)
            Next
            Return B
        End Function
    
    End Class
    

    【讨论】:

      猜你喜欢
      • 2014-03-25
      • 1970-01-01
      • 2021-03-27
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      • 2016-03-28
      • 1970-01-01
      • 2014-04-29
      相关资源
      最近更新 更多