1

I have a .bin file that needs to be read in, bit flipped, and written back to a binary file. For example:

RAW INPUT:    \xdd\x00 = 1101110100000000
FINAL OUTPUT: \x00\xbb = 0000000010111011

RAW INPUT:    \x33\x00 = 0011001100000000
FINAL OUTPUT: \x00\xcc = 0000000011001100

I'm having difficulties during this translation where some of the bytes will be translated and others will not. In the example above, I can easily flip the binary bits and get \x00\xbb from \xdd\x00, but the translation does not work for the \x33\00 to \x00\xcc

What I am currently doing is reading 1 byte in at a time, translating it into an integer, getting a binary value, flipping the bits, and then storing that to a new binary file.

data = binary_file.read(1)
while data:
    bits = ord(data)                        # Integer Value
    binary = ("{0:b}".format(bits))         # Binary value of Integer
    flipped = 0
    while bits:
        flipped <<=1
        flipped += bits & 1
        bits >>= 1
    data = binary_file.read(1)
    tmp = bytes([flipped])
    output_file.write(tmp)

Once I have the new file completed, I then open it back up, read two bytes in at a time, and flip them like so:

with open('H:/gitKraken/test.bin', 'rb') as tmp_file:
    print('Flipping Bytes')
    data = tmp_file.read(2)
    final_output = open('final.bin', 'wb')
    while data:
        flip = (data[0], data[1])
        final_output.write(bytes([flip[1]]))
        final_output.write(bytes([flip[0]]))
        data = tmp_file.read(2)

The values that I am getting are not what I was expecting:

This method works on the following bytes:
Raw Data: \x00\x00\xdd\x00
Expected (Wanted) Value: \x00\x00\x00\xbb
Final Data: \x00\x00\x00\xbb

However, the method does not work on the following bytes:
Raw Data: \x00\x00\x33\x00
Expected (Wanted) Value:: \x00\x00\x00\xcc
Final Data: \x00\x00\x003

Any ideas on how I can improve this process and make it actually work?


EDIT1:

Here is the effective process flow of how exactly I need to treat each byte and the end product:

Hex Bytes (Raw Data)      44           88          22          0
Binary Representation   01000100    10001000    00100010    00000000
Reverse Binary Order    00100010    00010001    01000100    00000000
New Hex Bytes              22          11          44          00
Flip Hex bytes(Final Data) 11          22          00          44

Here is another example using the bytes \x00\x00\xdd\x00

Hex Bytes (Raw Data)       00          00          dd          00
Binary Representation   00000000    00000000    11011101    00000000
Reverse Binary Order    00000000    00000000    10111011    00000000
New Hex Bytes              00          00          bb          00
Flip Hex bytes(Final Data) 00          00          00          bb
2
  • To clarify: you want to reverse the bits of every pair of bytes? Will your file always contain an even number of bytes? Approximately how large could a file be? Commented Mar 16, 2018 at 16:22
  • @ThierryLathuille Yes, I need to reverse the bits for every pair of bytes and write them to a new file. The final file size is close to 21MB. Commented Mar 16, 2018 at 16:25

1 Answer 1

1

You could do it like this:

with open('test.bin', 'rb') as f:
    data = f.read()

def reverse(b):
    return int('{:08b}'.format(b)[::-1], 2)

out = bytearray()   
for i in range(0, len(data), 2):
    out.extend((reverse(data[i+1]), reverse(data[i])))

with open('out.bin', 'wb') as f:
    f.write(out)

The reverse part is from nneonneo's answer to Reversing bits of Python integer

Sign up to request clarification or add additional context in comments.

5 Comments

That doesn't seem to be giving the correct output in out.bin file. For example, there are bytes: \x00\x00\xbb\x11\x22\x00\x44 which are the correct (expected values) that are required after the bit and byte reversal. However, the data present within the out.bin file is this: \x00\x00\x00\xdd\x88\x88\x00\x88
Please edit your question and include a short example file, and the corresponding output you expect. The code above does what you said you expect in your question. For example, with test.bin containing 00 00 DD 00 00 00 33 00, out.bin contains 00 00 00 BB 00 00 00 CC, which is exactly what one should expect according to your question.
I have added an edit with showing the process flow that each byte needs to go through from start to finish. The Raw Data is just that, the data that is pulled in from the Binary file and the Flipped Hex Bytes is the final structure I need the data to be in.
I checked it , and with test.bin containing 44 88 22 00 00 00 DD 00, I get out.bin containing 11 22 00 44 00 00 00 BB, which is exactly what you expect. So, this code does what it is supposed to do.
You're right, I changed a few things in my file strucutre so that was completlty on me. Sorry for the run around and thank you very very much for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.