extracting hex numbers from string

60 visualizaciones (últimos 30 días)
Adam
Adam el 28 de Feb. de 2012
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.

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (2)

the cyclist
the cyclist el 28 de Feb. de 2012
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 el 28 de Feb. de 2012
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});

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by