フィルターのクリア

convert double to signed int

15 ビュー (過去 30 日間)
michael
michael 2020 年 3 月 3 日
編集済み: Stephen23 2024 年 6 月 11 日
Hi,
Some doubt: if A=40000 = 0x9C40 and I'd like that 0x9C40 would be as signed integer - what shall be done?
executing int16, I'm getting
int16(A) = 32767
Any suggestion so that I'll get -25,536‬ ?

回答 (3 件)

Walter Roberson
Walter Roberson 2024 年 6 月 11 日
Depending on how the input is stated...
value = 0x9C40s16
value = int16 -25536

BhaTTa
BhaTTa 2024 年 6 月 11 日
To interpret 0x9C40 as a signed 16-bit integer (which in two's complement would indeed be -25536), you can follow this approach:
  1. Direct Interpretation (If the number is already in two's complement): If you have a number that is already in a form of two's complement (like 0x9C40 for a 16-bit system) and you want to interpret it as a signed integer, you can do the following:
% Your hexadecimal value
hexVal = '9C40';
% Convert hex to a 16-bit signed integer
signedVal = typecast(uint16(hex2dec(hexVal)), 'int16');
% Display the result
disp(signedVal);
This code snippet uses hex2dec to convert the hexadecimal string to a decimal. Since hex2dec returns a number that doesn't have a sign, it's first considered as uint16 (unsigned 16-bit integer). The typecast function is then used to reinterpret this number as int16 without changing its bit representation, allowing it to be interpreted as a signed integer.

Stephen23
Stephen23 2024 年 6 月 11 日
編集済み: Stephen23 2024 年 6 月 11 日
A = 40000
A = 40000
B = typecast(uint16(A), 'int16')
B = int16 -25536

カテゴリ

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

製品


リリース

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by