Pregunta

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?

¿Fue útil?

Solución

One way of doing it:

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

ans =

 -126

Otros consejos

do you need to do it in one line? If you know it is going to be interpreted as unsigned, you could just catch values greater than 127 in an if statement.

if (byte > 127)
    byte = byte - 256;
end
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top