필터 지우기
필터 지우기

extracting hex numbers from string

조회 수: 44 (최근 30일)
Adam
Adam 2012년 2월 28일
Hi guys
I have a string like this: 'Address: 0x050 Value: 0xEC'
I am recieving the string from a device, so i cant change the format of the numbers.
I want to extract the hexadecimal numbers from the string and convert them to decimal numbers!
How can i do this?
thx.

채택된 답변

Walter Roberson
Walter Roberson 2012년 2월 28일
s = 'Address: 0x050 Value: 0xEC';
out = sscanf(s,'Address: %x Value: %x');
This includes the conversion from hex to dec.
  댓글 수: 2
G A
G A 2012년 2월 28일
very elegant!
Andrei Bobrov
Andrei Bobrov 2012년 2월 28일
+1

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

추가 답변 (2개)

the cyclist
the cyclist 2012년 2월 28일
Depending on how consistent the placement of the hex value is, you might be able to simply use
s = 'Address: 0x050 Value: 0xEC';
hexValue = s(10:14);
but you might have to resort to using the regexp() command to extract the hex strings.
After you extract the hex string, you can use the function hex2dec() to convert it to a decimal value.

G A
G A 2012년 2월 28일
s = 'Address: 0x050 Value: 0xEC';
c = textscan(regexprep(s,'0x',''),'%s\t%s\t%s\t%s');
Address = hex2dec(c{2});
Value = hex2dec(c{4});

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by