MATLAB: How to convert 0xffff to -1

castint16negative numbers

I'm controlling a DAC with Matlab. I want to display the voltage that I'm setting the DAC to. The DAC voltage range is from -10 to 10v and the DAC treats the input value as a signed integer. If I cast 0xffff into int16 Matlab gives me maxint (32767) or if I cast into int32 I get 65535.
For example:
DACsetting = hex2dec('ffff')
%Display the setting
fprintf('%d\n', DACsetting * 10/(2^15 -1));
I can write buch of extra code to work around this, but this should be a very simple thing for a language to do.
DACsetting = hex2dec('ffff')
DACsetting =
65535
>> fprintf('%d\n', DACsetting * 10/(2^15-1))
2.000031e+01
fprintf('%d\n', int16(DACsetting) * 10/(2^15-1))
1
Thanks in advance.

Best Answer

typecast(uint16(hex2dec('ffff')), 'int16')