G
Guest
Hi
how can I compare two byte arrays in VB.NET
Thank
Peter
how can I compare two byte arrays in VB.NET
Thank
Peter
Cor said:\\\
Dim abyt1() As Byte = {12, 55, 88, 32}
Dim abyt2() As Byte = {12, 55, 87, 32}
If abyt1.Length = abyt2.Length Then
Dim mem1 As New IO.MemoryStream
Dim mem2 As New IO.MemoryStream
Dim binWriter1 As New IO.BinaryWriter(mem1)
Dim binWriter2 As New IO.BinaryWriter(mem2)
binWriter1.Write(abyt1)
binWriter2.Write(abyt2)
Dim binReader1 As New IO.BinaryReader(binWriter1.BaseStream)
Dim binReader2 As New IO.BinaryReader(binWriter2.BaseStream)
binReader1.BaseStream.Position = 0
binReader2.BaseStream.Position = 0
Dim a, b As String
a = binReader1.ReadChars(abyt1.Length)
b = binReader2.ReadChars(abyt2.Length)
If a <> b Then
MessageBox.Show("not equal char")
End If
Else
MessageBox.Show("not equal length")
End If
///
Peter said:how can I compare two byte arrays in VB.NET?
This code confuses bytes with characters, which is never a good idea.
In particular, not every byte array is going to be a valid stream of
UTF-8 encoded characters, at which point ReadChars will throw an
exception.
It also ends up using *4* times as much memory: it first copies all the
data into a stream, and then reads the data again into a character
array, which is going to take twice as much memory as the byte array.
Cor said:In the other direction I would agree with you, in this direction not.
A 8 bits byte becomes an 16 bits unicode, but the value stays the same.
When seeiing your message above I realize it even 6 times because the byte
is converted to uni as you said. However, that is exactly as I stated the
bad isue from this methode (Although I first thought it was 4 and said 2
because I thought I had miscalculated myself).
I stay with the same as Herfried showed as I said in my message, which is by
the way the same as yours, but because there was told that others where
better, I showed this as an other methode, which I probably myself never
shall use.
Cor said:I never take investigations if a byte will be converted to a 2 byte
character or not.
If not it is 4 times if it is converted to 2 bytes it is 6 times.
2 streams in memory
2 arrays from 2 bytes
Actualy it is not important, I only made this to show that when you really
want to do streaming than this would be "a" method.
As I said, I would never think about using this, however this did seem to me
to show that the normal comparising of a byte array as you, Herfried and I
am used to is probably the most sufficient.
The more because you can expect that in a byte array when there is a
difference than probably:
- the lenght is unequal
- it shows already with the first bytes, because that is the nature of a
byte array.
But there seems to be a lot of people who are thinking that when you do
looping in your program it is slow. (Although I think that it is probably
done in all the other methods behind the scene to get the same results).
The only tricky bit would be taking
into account that a Read from a stream might not return as much data as
you want it to.
Cor said:That one I was thinking later on, but a 00 byte in a string stays there so
the string will have that length but only is not showable.
(Although I have
thought to check if what I say above is true, however I think we said enough
about this). I never did bring it as an ideal methode, only if you real want
to do it without a for loop, this was also a possibility.