我如何比较 vb.net 中的字节数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19438732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i compare byte arrays in vb.net
提问by Afnan Makhdoom
Ok all i want to do is compare 2 byte arrays in vb.net . I tried these codes:
好的,我想要做的就是比较 vb.net 中的 2 个字节数组。我试过这些代码:
If Byte.ReferenceEquals(bytearrayone, bytearraytwo) Then
MsgBox("Yes", MsgBoxStyle.Information)
Else
MsgBox("No", MsgBoxStyle.Critical)
End If
And
和
If Array.ReferenceEquals(bytearrayone, bytearraytwo) Then
MsgBox("Yes", MsgBoxStyle.Information)
Else
MsgBox("No", MsgBoxStyle.Critical)
End If
Both byte arrays are the same, one array takes byte from a file from resources and the other from the computer. For testing purposes, I used the same file in both arrays but all I get is No according to the code provided. Both have the same lengths, i looped through both of them, both have same bytes at same points. Then what's wrong? What code should I use? Please help me.
两个字节数组是相同的,一个数组从资源中的文件中获取字节,另一个从计算机中获取字节。出于测试目的,我在两个数组中使用了相同的文件,但根据提供的代码,我得到的只是 No。两者都具有相同的长度,我遍历了它们,它们在相同的点都有相同的字节。那怎么了?我应该使用什么代码?请帮我。
回答by dbasnett
Use SequenceEqual
使用 SequenceEqual
Dim foo() As Byte = {1, 2, 3, 4}
Dim barT() As Byte = {1, 2, 3, 4}
Dim barF() As Byte = {1, 2, 3, 5}
Dim fooEqbarT As Boolean = foo.SequenceEqual(barT)
Dim fooEqbarF As Boolean = foo.SequenceEqual(barF)
Debug.WriteLine(fooEqbarT)
Debug.WriteLine(fooEqbarF)
Compare two small files
比较两个小文件
Dim path1 As String = "pathnameoffirstfile"
Dim path2 As String = "pathnameofsecondfile"
Dim foo() As Byte = IO.File.ReadAllBytes(path1)
Dim bar() As Byte = IO.File.ReadAllBytes(path2)
If foo.SequenceEqual(bar) Then
'identical
Else
'different
End If
回答by Steve
If your purpose is to compare 2 files to see if the contents are the same, without regards to the information about the file (ie, modified date, name, type, etc.) you should use a hash on both the files. Here is some code I have used for a while. There are many ways to do this.
如果您的目的是比较 2 个文件以查看内容是否相同,而不考虑有关文件的信息(即修改日期、名称、类型等),您应该对这两个文件使用哈希。这是我使用了一段时间的一些代码。有很多方法可以做到这一点。
''' <summary>
''' Method to get a unique string to idenify the contents of a file.
''' Works on any type of file but may be slow on files 1 GB or more and large files across the network.
''' </summary>
''' <param name="FI">System.IO.FileInfo for the file you want to process</param>
''' <returns>String around 50 characters long (exact length varies)</returns>
''' <remarks>A change in even 1 byte of the file will cause the string to vary
''' drastically so you cannot use this to see how much it differs by.</remarks>
Public Shared Function GetContentHash(ByVal FI As System.IO.FileInfo) As String
Dim SHA As New System.Security.Cryptography.SHA512Managed()
Dim sBuilder As System.Text.StringBuilder
Dim data As Byte()
Dim i As Integer
Using fs As New IO.FileStream(FI.FullName, IO.FileMode.Open)
data = SHA.ComputeHash(fs)
fs.Flush()
fs.Close()
End Using
sBuilder = New System.Text.StringBuilder
' Loop through each byte of the hashed data
' and format each one as a hexadecimal string.
For i = 0 To data.Length - 1
sBuilder.Append(data(i).ToString("x2"))
Next i
Return sBuilder.ToString()
End Function
You need to run this on each file, to come up with a string that uniquely identifies the file, then compare the 2 strings.
您需要在每个文件上运行它,以得出一个唯一标识该文件的字符串,然后比较这两个字符串。

