Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

converting binary to decimal

Asked by FIR on 19 Jan 2013

i have a value as S=-5

i converted it to binary by

Cw1=dec2bin(typecast(int16(S),'uint16'),15)

now how to convert again to -5

plz assist

0 Comments

FIR

Tags

Products

No products are associated with this question.

2 Answers

Answer by Image Analyst on 19 Jan 2013
Accepted answer

Why are you using uint16 when you have a negative number?????

Anyway,...

S = -5;
Cw1=dec2bin(typecast(int16(S),'uint16'),15)
S_recovered = bin2dec(Cw1)
maxUint16 = intmax('uint16')
maxInt16 = intmax('int16')
if S_recovered > maxInt16
	S_recovered = int16(S_recovered - double(maxUint16) - 1)
end

S_recovered will be -5 as a signed 16 bit integer, which is what I assume you want.

0 Comments

Image Analyst
Answer by Walter Roberson on 19 Jan 2013
typecast(bin2dec(Cw1), 'int16')

2 Comments

FIR on 21 Jan 2013

Walter i get

 typecast(bin2dec(Cw1), 'int16')

ans =

      0      0   -160  16623
Walter Roberson on 21 Jan 2013
typecast(uint16(bin2dec(Cw1)),'int16')
Walter Roberson

Contact us