Azzera filtri
Azzera filtri

convert decimal to hex with 3 digit

10 visualizzazioni (ultimi 30 giorni)
Jay
Jay il 20 Dic 2023
Commentato: Stephen23 il 20 Dic 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?

Risposta accettata

Chunru
Chunru il 20 Dic 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 Commenti
Jay
Jay il 20 Dic 2023
Modificato: Jay il 20 Dic 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 il 20 Dic 2023
dec2hex(-30,4)
ans = 'FFE2'
hex2dec('0xFFE2s16')
ans = -30

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Digital Input and Output in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by