Binary to Signed decimal in MATLAB

Hello,
I have a 64x1 workspace variable,where each value is of 16 bits.
I need to convert each of them into signed decimal value, which would span the entire range of signed 16 bit numbers from -32768 to +32767.
I tried bin2dec() but it provides only an unsigned value.
Kindly help.

3 Kommentare

Friedrich
Friedrich am 25 Mai 2012
How the values are stored? Is it two's complement (so sixteen 1 would be -1) or the classy way where sixteen ones would be -32768?
Can you give us one example and the value you expect?
Aparna
Aparna am 25 Mai 2012
Hello,
thanks for replying!
-32768 = 1000000000000000 , in our code.
We used the following line while converting into binary from decimal
k=dec2bin(typecast(int16(-32768),'uint16'))
Thank you for ur help
Aparna
Aparna am 25 Mai 2012
Basically, it is two's compliment conversion.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Friedrich
Friedrich am 25 Mai 2012

2 Stimmen

Hi,
can't you simply do the inverse way to get it back? Or am I missing something here?
typecast(uint16(bin2dec('1000000000000000')),'int16')

Weitere Antworten (2)

Titus Edelhofer
Titus Edelhofer am 25 Mai 2012

0 Stimmen

Hi,
then I guess the other way round should do the trick as well:
typecast(uint16(bin2dec('1000000000000000')), 'int16')
Titus
Aparna
Aparna am 25 Mai 2012

0 Stimmen

@ friedrich, Thanks a lot :)

Community Treasure Hunt

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

Start Hunting!

Translated by