As byte to bytes

Imperio Tuga 41 Reputation points
2021-10-17T11:07:30.99+00:00

Hello, ive this code:

Private Shared HexFind As Byte() = {&H1A, &H2A, &H3A, &H4A, &H5A}
Private Shared HexReplace As Byte() = {&H1B, &H2B, &H3B, &H4B, &H5B}

    Private Shared Function DetectHex(ByVal sequence As Byte(), ByVal position As Integer) As Boolean
        If position + HexFind.Length > sequence.Length Then Return False
        For p As Integer = 0 To HexFind.Length - 1
            If HexFind(p) <> sequence(position + p) Then Return False
        Next
        Return True
    End Function

How can i turn the above code into:

Private Shared HexFind As Byte() = {&H1A2A3A4A5A}
Private Shared HexReplace As Byte() = {&H1B2B3B4B5B}

Thanks all

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,580 questions
{count} votes

Accepted answer
  1. Dewayne Basnett 1,116 Reputation points
    2021-10-18T13:52:50.217+00:00

    Give this a try

    Private Shared HexReplace As Byte() = BitConverter.GetBytes(&H1B2B3B4B5B).Reverse.ToArray
    

    This should work,

    Private Shared HexFind As Byte() = HexStrToByteArray("1A2A3A4A5A") 'note missing &H
    Public Shared Function HexStrToByteArray(HexStr As String) As Byte()
        If (HexStr.Length And 1) = 1 Then 'even number of hex vals
            HexStr = HexStr.Insert(0, "0") 'no, insert 0
        End If
        Dim rv As New List(Of Byte)
        For x As Integer = 0 To HexStr.Length - 1 Step 2 'convert
            Dim b As Byte = Convert.ToByte(HexStr.Substring(x, 2), 16)
            rv.Add(b)
        Next
        Return rv.ToArray
    End Function
    

0 additional answers

Sort by: Most helpful