Converting 16-bit, little-endian, 2's complement integer to regular integer

10 ビュー (過去 30 日間)
Ben
Ben 2015 年 6 月 12 日
コメント済み: Walter Roberson 2015 年 6 月 14 日
I'm trying to get various data sets (accelerometer, gyroscope etc.) from a AHRS (a CHR-UM6) and I'm having trouble getting the values out in the correct form. The data received (for each value) is a two row column vector such as:
247
255
These numbers are 16-bit, little-endian, 2's complement and I need to get the actual value. I hope someone can help!
Thanks, Ben

採用された回答

Walter Roberson
Walter Roberson 2015 年 6 月 12 日
swapbytes(typecast(TheByteVector, 'int16'))
TheByteVector should be uint8.
  2 件のコメント
Ben
Ben 2015 年 6 月 13 日
At first I didn't think this was going to work as swapbytes just seems to switch between little and big endian and I was worried about the two's complement part, but I was pleasantly surprised when my output values were what they should've been, thanks. My code is as follows, for reference:
gyro_x = double(swapbytes(typecast((uint8(packet.data(1:2))),'int16')))*0.0610352;
This is used multiple times for different values. The input packet.data is a double. If there's a cleaner/quicker/more efficient way to do what I'm doing, please share, thanks.
Walter Roberson
Walter Roberson 2015 年 6 月 14 日
gyro_x = double(typecast(uint8(packet.data([2 1]),'int16')) * 0.0610352;
No need to call swapbytes if you don't need to ;-)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSensor Models についてさらに検索

タグ

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by