0

Its probably done in a few lines but im currently stuck on the following problem:

I want to transfer a matlab operation to python. Matlab looks like this:

startAddress = hex2dec('00D00000');
address = hex2dec(flipud((reshape(dec2hex(address, 8), 2, 4))'));

So i get a 2D-vector with decimal numbers out of this. The closest i got to transfer this to python is the following:

import numpy as np

startAddress = int('00D00000', 16)
address = startAddress.to_bytes(8, 'little')
address = np.frombuffer(addressMsg, dtype=np.uint8)
address = address[0:4].reshape(4,1)

Is this equivalent? The rest of my code doesnt work properly and its quite complex so i wanted to figure out if my mistake is here or somewhere deeper.

2
  • Can you share the expected output/output from Matlab? Commented Mar 2, 2020 at 18:08
  • Sure! It should be a 4x1 double array (code formatting in comments doesnt break lines; imagine a [0;0;208;0] array, quite simple..) : address = 0 0 208 0 Commented Mar 3, 2020 at 8:49

1 Answer 1

1

MATLAB and Python solutions are equivalent.

The only difference I noticed is that MATLAB result is of class double, and in Python the ndarray elements are of type uint8.
(The type differences are probably not relevant).

There are small issues with the sample code you have posted that makes it non-executable:

  • The MATLAB code should be: dec2hex(startAddress, 8) instead of dec2hex(address, 8).
  • The Python code should be: np.frombuffer(address, instead of np.frombuffer(addressMsg,.

You may get the same result using simpler code:

In MATLAB you can use typecast:

address = double(typecast(uint32(startAddress), 'uint8')');

Sample code for testing:

startAddress = hex2dec('12345678');
address = hex2dec(flipud((reshape(dec2hex(startAddress, 8), 2, 4))'));

address2 = double(typecast(uint32(startAddress), 'uint8')');

In Python you can use struct.pack:

address = np.frombuffer(struct.pack("<I", startAddress), np.uint8).reshape(4,1)

Sample code for testing:

startAddress = int('12345678', 16)
address = startAddress.to_bytes(8, 'little')
address = np.frombuffer(address, dtype=np.uint8)
address = address[0:4].reshape(4,1)

# Convert startAddress array of 4 uint8 elements (apply little endian format).
address2 = np.frombuffer(struct.pack("<I", startAddress), np.uint8).reshape(4,1)

MATLAB execution result:

address =

   120
    86
    52
    18

address2 =

   120
    86
    52
    18

Python execution result:

address
array([[120],
       [ 86],
       [ 52],
       [ 18]], dtype=uint8)

address2
array([[120],
       [ 86],
       [ 52],
       [ 18]], dtype=uint8)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much ! Youre right with the addressMsg instead of address part, of course, i wrote a function and forgot to mention that address = addressMsg. Trying out your simplification with struct right now :) thank you!

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.