【问题标题】:Increase Hex2dec or dec2hex output range in Matlab在 Matlab 中增加 Hex2dec 或 dec2hex 输出范围
【发布时间】:2013-08-29 10:15:41
【问题描述】:

我对 Matlab 中的 hex2dec 函数有一个奇怪的问题。 我意识到在 16bytes 数据中,它省略了 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 将其解释为 14byte + '00'。

【问题讨论】:

    标签: matlab simulink hex uint64


    【解决方案1】:

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

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

    返回

    B =
    
      1310867527582290490
    

    【讨论】:

    • 在simulink中试试这个,simulink仍然得到所有数据14bytes + 0x00
    • 你原来的问题没有提到 Simulink。 Simulink 不支持 uint64 (mathworks.se/help/simulink/ug/working-with-data-types.html)。您需要将其视为两个 32 位块。
    • @MohsenNosratinia 实际上,OP 确实提到了 Simulink:“我在 Simulink 中使用它。”我很早就在问题中添加了 Simulink 标签。
    • 所以他们在 simulink 中生成了 HD​​L 编码器,他们无法使用 64 位数据进行仿真?
    • @Ramyad 来自mathworks.co.uk/help/simulink/ug/working-with-data-types.html:“Simulink 支持除 int64 和 uint64 之外的所有内置数值 MATLAB 数据类型。”只能假设这些将在未来的版本中得到支持。
    【解决方案2】:

    在 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 does not support int64uint64 类型正如其他人已经注意到的那样......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多