4

Matlab 中的 hex2dec 函数有一个奇怪的问题。我意识到在 16 字节数据中,它省略了 2 个 LSB 字节。

hex2dec('123123123123123A');
dec2hex(ans)
Warning: At least one of the input numbers is larger than the largest integer-valued floating-point
number (2^52). Results may be unpredictable. 
ans =
1231231231231200

我在 Simulink 中使用它。因此我无法处理 16 字节的数据。Simulink 将其解释为 14 字节 + '00'。

4

2 回答 2

3

您需要使用uint64来存储该值:

A='123123123123123A';
B=bitshift(uint64(hex2dec(A(1:end-8))),32)+uint64(hex2dec(A(end-7:end)))

返回

B =

  1310867527582290490
于 2013-08-29T10:39:28.790 回答
0

在 MATLAB 中使用的另一种方法typecast

>> A = '123123123123123A';
>> B = typecast(uint32(hex2dec([A(9:end);A(1:8)])), 'uint64')
B =
  1310867527582290490

反之亦然:

>> AA = dec2hex(typecast(B,'uint32'));
>> AA = [AA(2,:) AA(1,:)]
AA =
123123123123123A

这个想法是将 64 位整数视为两个 32 位整数。

也就是说,Simulink不支持 其他人已经注意到的类型int64uint64

于 2013-08-30T02:54:11.903 回答