converting binary to decimal

3 vues (au cours des 30 derniers jours)
FIR
FIR le 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

Réponse acceptée

Image Analyst
Image Analyst le 19 Jan 2013
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.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 19 Jan 2013
typecast(bin2dec(Cw1), 'int16')
  2 commentaires
FIR
FIR le 21 Jan 2013
Walter i get
typecast(bin2dec(Cw1), 'int16')
ans =
0 0 -160 16623
Walter Roberson
Walter Roberson le 21 Jan 2013
typecast(uint16(bin2dec(Cw1)),'int16')

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by