MATLAB: How to convert a 24 bit, two’s complement value into a signed integer

MATLABspi interfacetwo's complement

I want to read an AD converter using the Raspberry Pi SPI interface controlled by Matlab. The AD converter outputs 24 bit data in the two's complement format, MSD first. The Matlab writeRead command returns the data as a row vector of data type char
How can I quickly translate the data into an integer of data type int32 ?

Best Answer

s = 2's complement 24-bit string to convert
b = [s([1 1 1 1 1 1 1 1]) s]; % sign extension
k = typecast(uint32(bin2dec(b)),'int32'); % equivalent decimal number