1

The file 'binary_file.bin' contains the following binary data (hexadecimal base used for simplicity):

A43CB90F

Each 2 bytes correspond to an unsigned integer of 16 bits: first number is A43C and second number is B90F, which in decimal base correspond respectively to 42044 and to 47375. I'm trying to read the binary input stream in Python using the method fromfile() from the numpy library as follows:

import numpy as np
binary_stream = open('binary_file.bin', 'rb')
numbers_to_read=2
numbers = np.fromfile(binary_stream,dtype=np.uint16,count=numbers_to_read,sep="") 
print(numbers[0]) # Result -----> DECIMAL: 15524 / HEXADECIMAL: 3CA4
print(numbers[1]) # Result -----> DECIMAL: 4025 / HEXADECIMAL:  0FB9

The first number read corresponds to 3CA4 instead of A43C, and the second number read corresponds to 0FB9 instead of B90F. So, it looks like the bytes are flipped when reading them with fromfile(). Is there any alternative to make sure that the bytes come in the same order as in the binary file? First number should be A43C (42044) and the second number should be B90F and the bytes shouldn't get flipped.

4
  • 1
    Cannot reproduce. Are you sure your file actually stores the 16-bit values in big-endian format, or are they little-endian (i.e., 3ca40fb9)? Commented Nov 19, 2022 at 17:03
  • What does binary_stream.read()[:4] produce immediately after you open the file? Commented Nov 19, 2022 at 17:09
  • @chepner The output is: b'\xa4<\xb9\x0f' Commented Nov 19, 2022 at 18:39
  • @chepner using a raw bytes visualiser I see that the content is H'A43CB90F. But my impression is that 3CA4 as first number is interpreted as little endian while A43C is interpreted as big endian. So, it's a question of how to change from little endian to big endian. EDIT: The file is indeed 2 bytes. Commented Nov 19, 2022 at 18:41

1 Answer 1

1

You need to specify the byteorder of the data type.

For example:

import numpy as np
binary_stream = open('/tmp/binary_file.bin', 'rb')
numbers_to_read=2
numbers = np.fromfile(binary_stream,
                      dtype=np.dtype('>H'),
                      count=numbers_to_read,
                      sep="") 
for num in numbers:
    print(f"Decimal = {num} | Hex = {num:04X}")

Which gives the following output:

Decimal = 42044 | Hex = A43C
Decimal = 47375 | Hex = B90F
Sign up to request clarification or add additional context in comments.

Comments

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.