Effacer les filtres
Effacer les filtres

How to add leading zeros to a number in Hex format?

18 vues (au cours des 30 derniers jours)
Mike
Mike le 9 Juil 2013
I'm trying to convert some binaries to the hexadecimal format. but I need Matlab to display zeros before the hex numbers.
for example:
if the input is '00001111' the output should display '0F'
if the input is '0000000000001011' the output should be '000B' and so on.
Thanks a lot for your help!

Réponse acceptée

Jan
Jan le 9 Juil 2013
Modifié(e) : Jan le 9 Juil 2013
s = '0000000000001011';
sprintf('%0*X', ceil(length(s)/4), bin2dec(s))

Plus de réponses (1)

dpb
dpb le 9 Juil 2013
MATL
>> bin2hex=@(x) dec2hex(bin2dec(x),length(x)/4)
bin2hex =
@(x)dec2hex(bin2dec(x),length(x)/4)
>> bin2hex=@(x) dec2hex(bin2dec(x),length(x)/4);
>> bin2hex('0000000000001011')
ans =
000B
>> bin2hex('1011')
ans =
B
>> bin2hex('00001011')
ans =
0B
>>

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by