How to convert 0xffff to -1?

조회 수: 10 (최근 30일)
Stephen Galperin
Stephen Galperin 2019년 6월 25일
댓글: Walter Roberson 2019년 6월 26일
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.

채택된 답변

Walter Roberson
Walter Roberson 2019년 6월 25일
typecast(uint16(hex2dec('ffff')), 'int16')
  댓글 수: 5
Stephen Galperin
Stephen Galperin 2019년 6월 26일
Thank you all. It looks like I'm going to have to store the DAC commands as a char array and convert when I need them. This was my work around but I had hoped their was some way to make Matlab convert 0xffff to -1. It looks like there isn't.
Thank you once again.
Walter Roberson
Walter Roberson 2019년 6월 26일
? You showed your input as character vector so that is what I programmed.
MATLAB has no direct 0xffff only '0xffff' or 'ffff' or binary values equivalent to 65535 integer, or double precision representation of 65535. What is your actual input?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by