MATLAB Answers

0

How can I show the 4-byte hex representation of a single precison float value?

Asked by glentall on 24 Jul 2019
Latest activity Commented on by glentall on 29 Jul 2019
Is there a way to show/display the 4-byte hex and/or binary representation of a single precision float value in Matlab and/or Simulink?

  0 Comments

Sign in to comment.

2 Answers

Answer by James Tursa
on 24 Jul 2019
Edited by James Tursa
on 24 Jul 2019
 Accepted Answer

s = your single float number
h = dec2hex(typecast(s,'uint32'),8)
And the reverse is
s = typecast(uint32(hex2dec(h)),'single')
If you want binary then see the dec2bin( ) and bin2dec( ) functions.

  2 Comments

Thank you - I will explore this!
I first tried your equations using the default variable type (double) at the Matlab command window and had erroneous results. Once I remembered to change 's' to a 'single,' I got the correct results that I desired.
Thanks James!

Sign in to comment.


Answer by Walter Roberson
on 24 Jul 2019

num2hex() gives a single call for this.

  0 Comments

Sign in to comment.