16 bit unsigned short byte flip in VB.NET

后端 未结 2 482
时光取名叫无心
时光取名叫无心 2021-01-29 11:44

What would be an example of byte flipping in VB.NET?

16 bit unsigned short

Before the flip:

02 00 0D 78 10 20 40 80 F1 F2 F4 F8          


        
2条回答
  •  天命终不由人
    2021-01-29 12:13

    You just need to loop through all items of the array and swap each byte.

    Dim bytes() As Byte = {&H2, &H0, &HD, &H78, &H10, &H20, &H40, &H80, &HF1, &HF2, &HF4, &HF8, &H1F, &H2F, &H4F, &H8F}
    
    For i As Integer = 0 To bytes.Length - 1 Step 2
        bytes(i) = bytes(i) Xor bytes(i + 1)
        bytes(i + 1) = bytes(i + 1) Xor bytes(i)
        bytes(i) = bytes(i) Xor bytes(i + 1)
    Next
    

    If you want, use a temp variable

    For i As Integer = 0 To bytes.Length - 1 Step 2
        Dim tempByte As Byte = bytes(i)
        bytes(i) = bytes(i+1)
        bytes(i + 1) = tempByte
    Next
    

提交回复
热议问题