4

I have a strange problem with hex2dec function in Matlab. I realized in 16bytes data, it omits 2 LSB bytes.

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 am using this in Simulink. Therefore I cannot process 16byte data. Simulink interpret this as a 14byte + '00'.

aurelius
  • 3,946
  • 7
  • 40
  • 73
Ramyad
  • 107
  • 1
  • 8

2 Answers2

3

You need to use uint64 to store that value:

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

which returns

B =

  1310867527582290490
Mohsen Nosratinia
  • 9,844
  • 1
  • 27
  • 52
  • try this in simulink, simulink still gets all data 14bytes + 0x00 – Ramyad Aug 29 '13 at 11:33
  • 1
    Well your original question didn't mention Simulink. Simulink doesn't have support for uint64 (http://www.mathworks.se/help/simulink/ug/working-with-data-types.html). You need to treat it as two 32bit chunks. – Mohsen Nosratinia Aug 29 '13 at 11:49
  • @MohsenNosratinia Actually the OP did mention Simulink: "I am using this in Simulink." I added the Simulink tag to the question early on. – am304 Aug 29 '13 at 11:55
  • So they produced HDL coder in simulink and they are not able to work with 64bit data for simulation? – Ramyad Aug 29 '13 at 12:00
  • @am304 I missed that. Sorry! – Mohsen Nosratinia Aug 29 '13 at 12:09
  • 1
    @Ramyad From http://www.mathworks.co.uk/help/simulink/ug/working-with-data-types.html: "Simulink supports all built-in numeric MATLAB data types except int64 and uint64." One can only assume that these will be supported in a future release. – am304 Aug 29 '13 at 12:18
  • @am304 Thanks very much. But, can I design a model which deals with 64bits data and convert it using HDL coder properly? I mean if I do not want to simulate it and only want to generate HDL code. – Ramyad Aug 30 '13 at 13:28
  • @Ramyad Not entirely sure, I'm not very familiar with HDL Coder. Better ask MathWorks for a definite answer. – am304 Aug 30 '13 at 13:36
0

An alternative way in MATLAB using typecast:

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

And the reverse in the opposite direction:

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

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

That said, Simulink does not support int64 and uint64 types as others have already noted..

Amro
  • 123,847
  • 25
  • 243
  • 454