MATLAB: How to create a single precision number given an ASCII HEX string in MATLAB 7.8 (R2009a)

32hex2numMATLABnum2hexsingleworkaround

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

Best 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