Search code examples
matlabbinarydecimalsigned

Convert binary to signed decimal


I'd like to convert a binary vector to a signed decimal in matlab using the classic functions.

I have a vector byte = [1 0 0 0 0 0 1 0],

if I convert it to decimal with byte = bi2de(byte, 'left-msb') I get 130, but if I insert this byte in a calculator I get -126, ergo signed.

I tried byte = typecast(bi2de(byte, 'left-msb'), 'int32'), but failed miserably.

How would I achieve this?


Solution

  • One way of doing it:

    >> typecast(uint8(bi2de(byte, 'left-msb')), 'int8')
    
    ans =
    
     -126