问题
Recently I've asked the help about converting numbers from decimal to other number-systems through MatLab. Now I need the help with the inverse operation. I wrote the program that converts from different systems to decimal and faced with the problem, when it comes to convert hex (base=16) to decimal. I need to make numbers like A=10, B=11 and so on. Tried to make switch-case, but it didn't help, even though it was working while converting from decimals
clc
clear all
n = input('select number system n=');
base = input('base= ', 's');
for q=1:length(base)
if ~(str2num(base(q))>=0 && str2num(base(q))<n)
error('Error.\nInput base must be greater than n.',class(n))
end
end
i = 0;
dec = 0;
while length(base)>i
i = i + 1;
dec = (dec+ str2num(base(i)) * n^(length(base) - i));
end
disp(['dec=' num2str(dec)])
来源:https://stackoverflow.com/questions/29326142/number-system-conversion-in-matlab-from-different-system-to-decimal