getting '0F' instead of 'F' for dec2hex(15)

조회 수: 1 (최근 30일)
Ronaldo
Ronaldo 2013년 10월 6일
댓글: Walter Roberson 2013년 10월 6일
I want to apply dec2hex function to a very large matrix. For a reason which is beyond the scope of this question, I need to get '0F' instead of 'F' for dec2hex(15). Since the running time is a matter, I want to find a time-efficient method to fix this problem.

채택된 답변

Jan
Jan 2013년 10월 6일
편집: Jan 2013년 10월 6일
dec2hex(15, 2)
% >> 0F
Of course such tricks are explained in the documentation ( help dec2hex ), so it is worth to read it before posting in the forum.
sprintf is slightly faster than dec2hex:
sprintf('%02x', 15)
x = randi([0, 255], 1, 1e3);
tic, for k=1:100, s = dec2hex(x, 2); end, toc
tic, for k=1:100, s = sprintf('%02x', x); end, toc
Elapsed time is 0.069916 seconds.
Elapsed time is 0.052959 seconds.
While dec2hex replies a CHAR-matrix, sprintf creates a CHAR vector, which might be more useful.
  댓글 수: 2
Ronaldo
Ronaldo 2013년 10월 6일
dec2hex(15)='F' while sprintf('%02x', 15)='0f'. I check that hex2dec('0F')==hex2dec('0f'). I was wondering if you confirm that there is no difference between capital and small letters in dec2hex and reverse conversion. Also is there anyway that I can get capital letters.
Walter Roberson
Walter Roberson 2013년 10월 6일
There is no difference between capital and small letters in hex2dec().
To get uppercase out of sprintf(), use X instead of x. %02X instead of %02x

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by