Skip to content

Instantly share code, notes, and snippets.

@avanwieringen
Created April 18, 2012 11:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avanwieringen/2413014 to your computer and use it in GitHub Desktop.
Save avanwieringen/2413014 to your computer and use it in GitHub Desktop.
Matlab convert numbers between different bases
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
function [ M ] = base2dec( a, base )
M = sum(a .* (base .^ (length(a)-1:-1:0)));
end
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