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?

有帮助吗?

解决方案

One way of doing it:

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

ans =

 -126

其他提示

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top