필터 지우기
필터 지우기

convert decimal to hex with 3 digit

조회 수: 12 (최근 30일)
Jay
Jay 2023년 12월 20일
댓글: Stephen23 2023년 12월 20일
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?

채택된 답변

Chunru
Chunru 2023년 12월 20일
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
Jay
Jay 2023년 12월 20일
편집: Jay 2023년 12월 20일
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 2023년 12월 20일
dec2hex(-30,4)
ans = 'FFE2'
hex2dec('0xFFE2s16')
ans = -30

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Digital Input and Output에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by