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
답변 (2개)
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
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
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'
Walter Roberson
2019년 8월 15일
typecast(uint16(cellfun(@(S) sscanf(S, '%x'), YourCell)), 'int16')
Walter Roberson
2012년 8월 18일
typecast( uint16( hex2dec(TheString) ), 'int16')
댓글 수: 1
Taha Zand
2022년 5월 30일
Thank you.(دمت گرم. بوس بوس)
카테고리
도움말 센터 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!