程序:
function string=ten2sixteen(num)
%the num should be a 1x3 Integer mat limited in [0 255]
exchange_list={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
string='#';
for i=1:3
temp_num=num(i);
string(1+i*2-1)=exchange_list{(temp_num-mod(temp_num,16))/16+1};
string(1+i*2)=exchange_list{mod(temp_num,16)+1};
end
end
效果: 转换回去程序:
function num=sixteen2ten(string)
exchange_list='0123456789ABCDEF#';
num=zeros(1,3);
for i=1:3
tempCoe1=find(exchange_list==string(i*2))-1;
tempCoe2=find(exchange_list==string(i*2+1))-1;
num(i)=16*tempCoe1+tempCoe2;
end
end
效果: