getting '0F' instead of 'F' for dec2hex(15)
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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 个评论
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!