How can I convert a hexadecimal string from two's complement to a signed int?

Hello,
In a CCS code that i have produced, my data comes in two's complement, wich is stored as a hexadecimal number.
Than i send it to matlab by serial port, and it's read as a string (example: 'FFDF').
I know that FFDF is equal to -33, but how do i make that convertion in matlab?
Thank you,
Josué Jones

1 件のコメント

Jan
Jan 2012 年 8 月 18 日
編集済み: Jan 2012 年 8 月 18 日
In opposite to some other binary/hex conversion questions in this forum, you provided an example value, which unequivocally defines, what you want. Therefore I've voted this question - and it is my personal question of the week!

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

回答 (2 件)

Jan
Jan 2012 年 8 月 18 日
Similar to Walter's method, but the builtin function hex2dec is slow, while sscanf converts hexadecimal values very fast:
typecast(uint16(sscanf('FFDF', '%x')), 'int16')

3 件のコメント

Jonathon
Jonathon 2014 年 12 月 29 日
This was tremendously useful for converting my fixed-point coefficients from fixed-point matlab objects to standard integers. Exactly what I needed. Thanks so much!
manoj hanu
manoj hanu 2019 年 8 月 15 日
Is there a possibility of using the same on a complete column of 16bit Hex values in a table ?? DataType being 'cell array of character vectors'
typecast(uint16(cellfun(@(S) sscanf(S, '%x'), YourCell)), 'int16')

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

Walter Roberson
Walter Roberson 2012 年 8 月 18 日
typecast( uint16( hex2dec(TheString) ), 'int16')

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

質問済み:

2012 年 8 月 18 日

コメント済み:

2022 年 5 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by