convert decimal to hex with 3 digit

8 Ansichten (letzte 30 Tage)
Jay
Jay am 20 Dez. 2023
Kommentiert: Stephen23 am 20 Dez. 2023
I just realized that converting -30 to hexadecial by dec2hex(-30) is different from its converting back by hex2dec('E2'). Any suggestion donig right?
dec2hex(-30)
ans = 'E2'
hex2dec('E2')
ans = 226
Also if I want to specify the decimal in signed 12 bit (e.g., 3 hex digital), what am I supposed to do?

Akzeptierte Antwort

Chunru
Chunru am 20 Dez. 2023
dec2hex(-30) % this converts to hex in 2's complement
ans = 'E2'
hex2dec('E2') % this treats the hex as unsigned by default
ans = 226
hex2dec('0xE2s8') % s8 to indicate the hex is a signed 8 bit nmber; 0x prefix is also needed
ans = -30
  2 Kommentare
Jay
Jay am 20 Dez. 2023
Bearbeitet: Jay am 20 Dez. 2023
Thanks a lot! This partially resolve my issues. What am I supposed to do if I convert a signed 12-bit to hexadecimal number or vice versa?
To be more specific, my problem is follow.
I have list of decimal number which can be represented by signed 12-bit. Simply converting them using dec2hex gives various word lengths (e.g., 2 or 3 hex digits some time and 4 hex digits for some negative numbers). Any way to convert to 3-hex digit which is corresponding to signed 12 bits? Otherwise, the indication of the signed bit width in your solution (e.g., hex2dec('0xE2s8') only for partially and 3 or 4 hex digit numbers have to be processed separately.
Stephen23
Stephen23 am 20 Dez. 2023
dec2hex(-30,4)
ans = 'FFE2'
hex2dec('0xFFE2s16')
ans = -30

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Digital Input and Output finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by