필터 지우기
필터 지우기

Convert string of hex to array of dec

조회 수: 1 (최근 30일)
John Ramshur
John Ramshur 2011년 8월 17일
I need to convert a string of hexidecimal numbers to an array of dec numbers. The string can potentially contain incomplete hex numbers at the beginning and end of the string. See examples below. I'm trying to use this in a data acquisition setup...so execution time could be a factor.
Input String Examples ('/r' = carriage returns:
s1 = '0001h/r0002h/r0003h/r';
s2 = '4h/r0001h/r0002h/r0003h/r00';
Output Desired:
a1 = [1,2,3]
a2 = [1,2,3]

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 8월 17일
s1 = '0001h/r0002h/r0003h/r';
s1 = '4h/r0001h/r0002h/r0003h/r00';
str=regexp(s1,'h/r','split');
Ind=cellfun(@length,str);
str=str(Ind==4);
num=hex2dec(str);
  댓글 수: 2
John Ramshur
John Ramshur 2011년 8월 17일
Worked perfectly. Thanks.
Jan
Jan 2011년 8월 17일
cellfun('prodofsize', str) is faster than cellfun(@length, str).

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2011년 8월 17일
A simpler method, which is substantially faster:
s1 = '0001h/r0002h/r0003h/r';
a1 = sscanf(a, '%xh/r')
But this does not reject the 4 of the string '4h/r0001h/r0002h/r0003h/r00'!
If you mean \r instead /r:
s1 = sprintf('0001h\r0002h\r0003h\r');
a1 = sscanf(a, '%xh')

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by