문제

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