Created
April 18, 2012 11:33
-
-
Save avanwieringen/2413014 to your computer and use it in GitHub Desktop.
Matlab convert numbers between different bases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [ b ] = base2base( a, base_from, base_to ) | |
M = base2dec(a, base_from); | |
n = floor(log10(M) / log10(base_to)); | |
b = zeros(1, n+1); | |
for i = 0:n; | |
b(n + 1 - i) = mod(floor(M / (base_to^i)), base_to); | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [ M ] = base2dec( a, base ) | |
M = sum(a .* (base .^ (length(a)-1:-1:0))); | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [ a ] = dec2base( dec, base ) | |
a = base2base(sscanf( sprintf( '%u', dec ), '%1d' )', 10, base); | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment