hatzisn Expert Licensed User Longtime User Jan 29, 2021 #1 Is there a direct way to compare two byte arrays other than looping in their incredients? I send two bytes from the computer and in the Mobile Part I am receiving them correctly but if I check the following B4X: 'Obviously comparing objects If Buffer = "Ok".GetBytes("UTF8") Then End if it always returns false although I send "Ok".GetBytes("UTF8") . Last edited: Jan 29, 2021
Is there a direct way to compare two byte arrays other than looping in their incredients? I send two bytes from the computer and in the Mobile Part I am receiving them correctly but if I check the following B4X: 'Obviously comparing objects If Buffer = "Ok".GetBytes("UTF8") Then End if it always returns false although I send "Ok".GetBytes("UTF8") .
Magma Expert Licensed User Longtime User Jan 29, 2021 #2 B4X: If BUFFER.GetBytes("UTF8") = "Ok".GetBytes("UTF8") Then .... End If I think... Upvote 0
Magma Expert Licensed User Longtime User Jan 29, 2021 #3 Hey... you are Greek too https://www.b4x.com/android/forum/t...evelopers-lets-get-to-know-each-other.127034/ Upvote 0
Hey... you are Greek too https://www.b4x.com/android/forum/t...evelopers-lets-get-to-know-each-other.127034/
stevel05 Expert Licensed User Longtime User Jan 29, 2021 #4 B4X: If Buffer = "Ok".GetBytes("UTF8") Comparing for equality like that will test if it the same byte array, not it's contents. You could use the ByteConverter library B4X: Dim B() As Byte = "OK".GetBytes("UTF8") Dim BC As ByteConverter Log(BC.StringFromBytes(B,"UTF8") = "OK") Or java.util.Array class via Javaobject: : B4X: Dim B() As Byte = "OK".GetBytes("UTF8") Dim Jo As JavaObject Jo.InitializeStatic("java.util.Arrays") Log(Jo.RunMethod("equals",Array(B,"OK".GetBytes("UTF8")))) Last edited: Jan 29, 2021 Upvote 0
B4X: If Buffer = "Ok".GetBytes("UTF8") Comparing for equality like that will test if it the same byte array, not it's contents. You could use the ByteConverter library B4X: Dim B() As Byte = "OK".GetBytes("UTF8") Dim BC As ByteConverter Log(BC.StringFromBytes(B,"UTF8") = "OK") Or java.util.Array class via Javaobject: : B4X: Dim B() As Byte = "OK".GetBytes("UTF8") Dim Jo As JavaObject Jo.InitializeStatic("java.util.Arrays") Log(Jo.RunMethod("equals",Array(B,"OK".GetBytes("UTF8"))))
hatzisn Expert Licensed User Longtime User Jan 29, 2021 #5 Here is a workaround I came with because I want to use it also in B4i. The string comparison I think that would also work but I couldn't risk loosing non printable characters (in case something like that would happen). Here is the solution : Code: Private Sub CompareByteArrays(b1() As Byte, b2() As Byte) As Boolean If b1.Length <> b2.Length Then Return False Dim ii As Long For ii = 0 To b1.Length - 1 If b1(ii) <> b2(ii) Then Return False End If Next Return True End Sub Upvote 0
Here is a workaround I came with because I want to use it also in B4i. The string comparison I think that would also work but I couldn't risk loosing non printable characters (in case something like that would happen). Here is the solution : Code: Private Sub CompareByteArrays(b1() As Byte, b2() As Byte) As Boolean If b1.Length <> b2.Length Then Return False Dim ii As Long For ii = 0 To b1.Length - 1 If b1(ii) <> b2(ii) Then Return False End If Next Return True End Sub
Erel B4X founder Staff member Licensed User Longtime User Jan 31, 2021 #6 You can also use B4XBytesBuilder (B4XCollections). Check the length and then check whether IndexOf returns 0. Upvote 0
You can also use B4XBytesBuilder (B4XCollections). Check the length and then check whether IndexOf returns 0.