Increase Hex2dec or dec2hex output range in Matlab

I have a strange problem with the hex2dec function in Matlab. I understood in 16 byte data, it skips 2 bytes of 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 

I use this in Simulink. Therefore, I cannot process 16 byte data. Simulink interprets this as 14byte + '00'.

+4
source share
2 answers

You need to use uint64 to save this value:

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

which returns

 B = 1310867527582290490 
+3
source

An alternative way to use MATLAB with typecast :

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

And vice versa in the opposite direction:

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

The idea is to treat a 64-bit integer as two 32-bit integers.

However, Simulink does not support the int64 and uint64 , as others have already noted.

0
source

Source: https://habr.com/ru/post/1499583/


All Articles