フィルターのクリア

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

24 ビュー (過去 30 日間)
Mike
Mike 2013 年 7 月 9 日
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!

採用された回答

Jan
Jan 2013 年 7 月 9 日
編集済み: Jan 2013 年 7 月 9 日
s = '0000000000001011';
sprintf('%0*X', ceil(length(s)/4), bin2dec(s))

その他の回答 (1 件)

dpb
dpb 2013 年 7 月 9 日
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
>>

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by