0

I have a MATLAB application that reads a .bin file and parses through the data. I am trying to convert this script from MATLAB to Python but am seeing discrepancies in the values being read.

The read function utilized in the MATLAB script is:

fname = 'file.bin';
f=fopen(fname);
data = fread(f, 100);
fclose(f);

The Python conversion I attempted is: (edited)

fname = 'file.bin'
with open(fname, mode='rb') as f:
    data= list(f.read(100))

I would then print a side-by-side comparison of the read bytes with their index and found discrepancies between the two. I have confirmed that the values read in Python are correct by executing $ hexdump -n 100 -C file.bin and by viewing the file's contents on the application HexEdit.

I would appreciate any insight into the source of discrepancies between the two programs and how I may be able to resolve it.

Note: I am trying to only utilize built-in Python libraries to resolve this issue.

Solution: Utilizing incorrect file path/structure between programming languages. Implementing @juanpa.arrivillaga's suggestion cleanly reproduced the MATLAB results.

2
  • Um, this is completely redundant: int(hex(ord(i)),16) can just be ord(i), IOW, int(hex(whatever), 16) == whatever Commented Nov 12, 2022 at 19:29
  • Also, data = [int(hex(ord(i)),16) for i in bytes] would raise a TypeError, because i is an int, and ord(i) is expecting a str of length 1. You really must provide a minimal reproducible example Commented Nov 12, 2022 at 19:31

2 Answers 2

1

An exact translation of the MATLAB code, using NumPy, would be:

data = np.frombuffer(f.read(100), dtype=np.uint8).astype(np.float64)
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, this reproduced the same results as @juanpa.arrivillaga suggested, except as a float64.
@Alex This is exactly MATLAB’s behavior. Are you sure you get a different result in MATLAB? Are you sure you’re referencing the same file? If things really don’t match up with the same file, please include the file in your post (or at least the hex dump of the first 100 values), and show us what MATLAB’s output is and what Python’s output is.
0

python automatically transforms single bytes into unsigned integers, as done by matlab, so you just need to do the following.

fname = 'file.bin'
with open(fname, mode='rb') as f:
    bytes_arr = f.read(100)
    # Conversion for visual comparison purposes
    data = [x for x in bytes_arr]
print(data)

also welcome to python, bytes is a built-in type, so please don't override the built-in bytes type ... or you'll run into unexpected problems.

Edit: as pointed by @juanpa.arrivillaga you could use the faster

fname = 'file.bin'
with open(fname, mode='rb') as f:
    bytes_arr = f.read(100)
    # Conversion for visual comparison purposes
    data = list(bytes_arr)

3 Comments

data = [x for x in bytes_arr] -> list(bytes_arr)
Thank you for your suggestions. This does improve the clarity of the code but, does not resolve the underlying issue of the read discrepancies.
@Alex it does solve the underlying issue of discrepancies, if you are getting different results then the problem is not in this block of code.

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.