这是我参考MATLAB函数改写后的函数,基本可以满足你的要求,支持11字节的16进制数的string或string array或
cell array输入和相应的输出并且具有检错功能,程序健壮性良好
function d=your_solution(h)
%YOUR_SOLUTION Convert 11-bytes hexadecimal string to your specific decimal integer.
% D = your_solution(H) interprets the 11-byte hexadecimal string H and returns in D the
% equivalent decimal number.
%
% If H is a character array or cell array of strings, each row is interpreted
% as a 11-bytes hexadecimal string.
%
% EXAMPLES:
% your_solution('E3AABBCCDDEEFF00998877') and your_solution('e3aabbccddeeff00998877')
% both return 65518
%
% Reference MATLAB function: DEC2HEX, HEX2NUM, BIN2DEC, BASE2DEC.
% Author: Neptune_zx(www.ilovematlab.cn): 10-30-2010, HFUT.
if iscellstr(h), h = char(h); end
if isempty(h), d = []; return, end
% Work in upper case.
h = upper(h);
[m,n]=size(h);
if(n~=22)
error( 'Does not support the hexadecimal string of non 11-bytes');
end
% Right justify strings and form 2-D character array.
if ~isempty(find((h==' ' | h==0),1))
error([ 'there are some non 11-bytes hexadecimal strings which do not support,'...
'be sure your strings are all 11-bytes'])
end
% Check for out of range values
if any(any(~((h>='0' & h<='9') | (h>='A'&h<='F'))))
error('Input string found with characters other than 0-9, a-f, or A-F.');
end
h = reshape(h,m,n);
h=h(:,16:-1:11);
n=6;
%compute the results and returns in d
sixteen = 16;
p = fliplr(cumprod([1 sixteen(ones(1,n-1))]));
p = p(ones(m,1),:);
d = h <= 64; % Numbers
h(d) = h(d) - 48;
d = h > 64; % Letters
h(d) = h(d) - 55;
d = sum(h.*p,2);
%=====================================================================
EXAMPLE:
>> your_solution({'E3AABBCCDDDEFFF0f98877','AABBCCDDEEFF11223344556677'})
??? Error using ==> your_solution at 26
Does not support the hexadecimal string of non 11-bytes
>> your_solution({'E3AABBCCDDDEFFF0f98877','AABBCCDDEEFF1122334455'})
ans =
1048557
2232831
[本帖最后由 Neptune_zx 于 2010-10-30 20:19 编辑]