Number-system conversion in Matlab (from different system to decimal)

一笑奈何 提交于 2019-12-12 02:19:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!