# convert decimal to hex with 3 digit

16 vues (au cours des 30 derniers jours)
Jay le 20 Déc 2023
Commenté : Stephen23 le 20 Déc 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?
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Chunru le 20 Déc 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 commentairesAfficher AucuneMasquer Aucune
Jay le 20 Déc 2023
Modifié(e) : Jay le 20 Déc 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 le 20 Déc 2023
dec2hex(-30,4)
ans = 'FFE2'
hex2dec('0xFFE2s16')
ans = -30

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Digital Input and Output dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by