Borrar filtros
Borrar filtros

convert decimal to hex with 3 digit

12 visualizaciones (últimos 30 días)
Jay
Jay el 20 de Dic. de 2023
Comentada: Stephen23 el 20 de Dic. de 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?

Respuesta aceptada

Chunru
Chunru el 20 de Dic. de 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 comentarios
Jay
Jay el 20 de Dic. de 2023
Editada: Jay el 20 de Dic. de 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 el 20 de Dic. de 2023
dec2hex(-30,4)
ans = 'FFE2'
hex2dec('0xFFE2s16')
ans = -30

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Digital Input and Output en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by