带* .bin文件的无符号字节翻转

时间:2015-01-28 15:04:49

标签: vb.net

我需要代码中的示例,如何字节翻转整个二进制文件。 VB.NET

实施例

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

翻转操作

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

使用OpenFileDialog(OFD)的整个二进制文件

2 个答案:

答案 0 :(得分:0)

您只需循环数组的所有项目并交换每个字节。

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

如果需要,请使用临时变量

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

答案 1 :(得分:0)

嗯,你可以像往常一样写,

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