简体   繁体   English

字节>字符串>字节>文件VB

[英]Byte > String > Byte > File VB

I would like to convert byte to string and back, I've tried this: 我想将字节转换为字符串然后返回,我已经尝试过了:

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

The new file that was created was corrupted. 创建的新文件已损坏。 Can you please recommend any other solutions? 您能推荐其他解决方案吗?

Sorry for my bad English, it ain't my main language. 对不起,我的英语不好,这不是我的主要语言。

Your byte to string conversion is wrong. 您的字节到字符串的转换是错误的。 You need to use: 您需要使用:

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

Source: 资源:

How to: Convert an Array of Bytes into a String in Visual Basic 如何:在Visual Basic中将字节数组转换为字符串

How to: Convert Strings into an Array of Bytes in Visual Basic 如何:在Visual Basic中将字符串转换为字节数组


You might want to read this as well to decide which encoding to use: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) 您可能还需要阅读一下,以决定使用哪种编码: 每个软件开发人员绝对,肯定地必须绝对了解Unicode和字符集(无借口!)

I've found an answer, the new code is: 我找到了答案,新代码是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM