how to change a number of base n to base m

24 views (last 30 days)
i want to write a code that changes the base b of a number
for example :
124 can be written as 01444 in base of 5
112 can be written as 01300 in base of 4
the input should be a numbe x in decimal base and a new base b
the output should be the number x in base b

Accepted Answer

the cyclist
the cyclist on 6 Sep 2019
Edited: the cyclist on 6 Sep 2019
John D'Errico created the base2base function to do exactly this. Follow that link to the File Exchange.
>> base2base('124',10,5)
ans =
'444'
(I think you got your example wrong.)

More Answers (1)

Bruno Luong
Bruno Luong on 6 Sep 2019
Edited: Bruno Luong on 6 Sep 2019
>> dec2base(124,5)
ans =
'444'
>> dec2base(122,4)
ans =
'1322'
>> base2dec('1444',5)
ans =
249
>> base2dec('1300',4)
ans =
112
>> dec2base(base2dec('1300',4),5) % convert 1300 in base 4 to base 5
ans =
'422'
>>

Products

Community Treasure Hunt

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

Start Hunting!