MATLAB Answers

0

How can I create a single precision number given an ASCII HEX string in MATLAB 7.8 (R2009a)?

I would like to create a single precision number given an ASCII hex string.

Products


Release

R2008a

1 Answer

Answer by MathWorks Support Team on 27 Jun 2009
 Accepted Answer

Since the HEX2NUM only supports DOUBLE precision data, the following is a workaround for using only built-in functions to allow HEX2NUM type functionality to extent to SINGLE data.
% first create an example
str=num2hex(single(-pi));
% now convert the string of ascii HEX characters back into a single precsion number
n=typecast(uint32(hex2dec(str)),'single');
% note that eps(n) is on the order of the error for -pi-n