Matlab 到 Python;将十六进制地址转换为十进制 numpy 数组
Matlab to Python; Converting a hex-address to a decimal numpy array
它可能在几行内完成,但我目前遇到以下问题:
我想转一个matlab操作到python。 Matlab 看起来像这样:
startAddress = hex2dec('00D00000');
address = hex2dec(flipud((reshape(dec2hex(address, 8), 2, 4))'));
所以我从中得到了一个带有十进制数的二维向量。我最接近将其转移到 python 的如下:
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)
这是等价的吗?我的其余代码无法正常工作并且非常复杂,所以我想弄清楚我的错误是在这里还是更深层次的地方。
MATLAB 和 Python 解是等价的。
我注意到的唯一区别是 MATLAB 结果是 class double
,而在 Python 中,ndarray 元素的类型是 uint8
.
(类型差异可能不相关)。
您发布的示例代码存在一些小问题,导致无法执行:
- MATLAB 代码应该是:
dec2hex(startAddress, 8)
而不是 dec2hex(address, 8)
。
- Python 代码应为:
np.frombuffer(address,
而不是 np.frombuffer(addressMsg,
。
您可能会使用更简单的代码得到相同的结果:
在 MATLAB 中你可以使用 typecast
:
address = double(typecast(uint32(startAddress), 'uint8')');
测试示例代码:
startAddress = hex2dec('12345678');
address = hex2dec(flipud((reshape(dec2hex(startAddress, 8), 2, 4))'));
address2 = double(typecast(uint32(startAddress), 'uint8')');
在Python中你可以使用struct.pack
:
address = np.frombuffer(struct.pack("<I", startAddress), np.uint8).reshape(4,1)
测试示例代码:
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执行结果:
address =
120
86
52
18
address2 =
120
86
52
18
Python执行结果:
address
array([[120],
[ 86],
[ 52],
[ 18]], dtype=uint8)
address2
array([[120],
[ 86],
[ 52],
[ 18]], dtype=uint8)
它可能在几行内完成,但我目前遇到以下问题:
我想转一个matlab操作到python。 Matlab 看起来像这样:
startAddress = hex2dec('00D00000');
address = hex2dec(flipud((reshape(dec2hex(address, 8), 2, 4))'));
所以我从中得到了一个带有十进制数的二维向量。我最接近将其转移到 python 的如下:
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)
这是等价的吗?我的其余代码无法正常工作并且非常复杂,所以我想弄清楚我的错误是在这里还是更深层次的地方。
MATLAB 和 Python 解是等价的。
我注意到的唯一区别是 MATLAB 结果是 class double
,而在 Python 中,ndarray 元素的类型是 uint8
.
(类型差异可能不相关)。
您发布的示例代码存在一些小问题,导致无法执行:
- MATLAB 代码应该是:
dec2hex(startAddress, 8)
而不是dec2hex(address, 8)
。 - Python 代码应为:
np.frombuffer(address,
而不是np.frombuffer(addressMsg,
。
您可能会使用更简单的代码得到相同的结果:
在 MATLAB 中你可以使用 typecast
:
address = double(typecast(uint32(startAddress), 'uint8')');
测试示例代码:
startAddress = hex2dec('12345678');
address = hex2dec(flipud((reshape(dec2hex(startAddress, 8), 2, 4))'));
address2 = double(typecast(uint32(startAddress), 'uint8')');
在Python中你可以使用struct.pack
:
address = np.frombuffer(struct.pack("<I", startAddress), np.uint8).reshape(4,1)
测试示例代码:
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执行结果:
address =
120
86
52
18
address2 =
120
86
52
18
Python执行结果:
address
array([[120],
[ 86],
[ 52],
[ 18]], dtype=uint8)
address2
array([[120],
[ 86],
[ 52],
[ 18]], dtype=uint8)