-2

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 1F 2F 4F 8F

After the flip it should be:

00 02 78 0D 20 10 80 40 F2 F1 F8 F4 2F 1F 8F 4F

It should flip the whole *.bin file using OFD.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Some people advise [abusing `HostToNetworkOrder`](http://stackoverflow.com/a/23817667/11683), some people advise using bitwise logic (obviously, better done with [bit shift operators](http://stackoverflow.com/a/8241150/11683) that are also [found in VB.NET](https://msdn.microsoft.com/en-us/library/2d9yb87a.aspx) rather than [in the VBA way](http://stackoverflow.com/a/2667033/11683)). – GSerg Jan 27 '15 at 20:33
  • But it should, flip whole file. Not only few offsets – Ymourok2 Jan 27 '15 at 20:37
  • Then use a 'loop' to apply the logic to each of the shorts in the file. – GSerg Jan 27 '15 at 20:39
  • 1
    Just read 2 bytes with BinaryReader, write those 2 bytes in reverse with BinaryWriter. And loop. Not that many real files are structured like that. Well, just about none do. – Hans Passant Jan 27 '15 at 20:43

2 Answers2

0

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
the_lotus
  • 12,668
  • 3
  • 36
  • 53
  • 1
    Using a temporary variable is likely to lead to the compiler recognising the intent and optimising the generated code. The XOR technique is, in general, best avoided: [Reasons for avoidance in practice](https://en.wikipedia.org/wiki/XOR_swap_algorithm#Reasons_for_avoidance_in_practice). – Andrew Morton Sep 09 '16 at 17:16
0

You could write as you go, something like:

Dim file = dialog.FileName

Using output = New BinaryWriter(New FileStream( _
                                  file, _
                                  FileMode.Append, _
                                  FileAccess.Write, _
                                  FileShare.Read))

    Using input = New BinaryReader(New FileStream( _
                                     file, _
                                     FileMode.Open, _
                                     FileAccess.Read, _
                                     FileShare.ReadWrite))
        Dim bufferLength = 2
        While bufferLength = 2
            Dim buffer = input.ReadBytes(2)
            bufferLength = buffer.Length
            If bufferLength = 2 Then
                output.Write(buffer(1))
                output.Write(buffer(0))
            Else If bufferLength = 1 Then
                output.Write(buffer(0))
            End If
        End While
    End Using
End Using
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jodrell
  • 34,946
  • 5
  • 87
  • 124