How can I convert a hexadecimal string from two's complement to a signed int?
Ältere Kommentare anzeigen
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 Kommentar
Antworten (2)
Jan
am 18 Aug. 2012
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 Kommentare
Jonathon
am 29 Dez. 2014
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
am 15 Aug. 2019
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'
Walter Roberson
am 15 Aug. 2019
typecast(uint16(cellfun(@(S) sscanf(S, '%x'), YourCell)), 'int16')
Walter Roberson
am 18 Aug. 2012
typecast( uint16( hex2dec(TheString) ), 'int16')
1 Kommentar
Taha Zand
am 30 Mai 2022
Thank you.(دمت گرم. بوس بوس)
Kategorien
Mehr zu Data Type Conversion finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!