matlab取高字节,求助!如何将11字节的数提取出其中678三个字节

这是我参考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 编辑]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值