Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract hex number from string python

I’m trying to extract hex number from this string (type str) :

mystring = b'\r\n+CUSD: 2,"062506460642063706270639002006270644062E062F06450629002006230639062F0020062706440625062A063506270644",72\r\n\r\nOK\r\n'

I tried:

hexnumber= m = re.findall(r'[0-9a-fA-F]+' , mystring)
print(hexnumber)

Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

['b', 'C', 'D', '1', '0637064406280020063A064A0631002006450648062C0648062F000A002D0020002D0020002D000A00300030003A0627064406420627062606450629000A0030003A0631062C06480639', '72']

The output i’m looking for is :

0637064406280020063A064A0631002006450648062C0648062F000A002D0020002D0020002D000A00300030003A0627064406420627062606450629000A0030003A0631062C06480639

>Solution :

You "string" is not a string with actually bytes (b'…'), so you should probably decode according to the used encoding (I assumed utf-8 here).

Then I also assumed you want to extract the string in between quotes, so I suggest using a regex with lookarounds:

import re
out = re.findall(r'(?<=")[a-fA-F\d]+(?=")', mystring.decode('utf-8'))
if out:
    print(out[0])

# 062506460642063706270639002006270644062E062F06450629002006230639062F0020062706440625062A063506270644

You can also set a minimum number of characters in your pattern (here 8 or more):

re.findall(r'[a-fA-F\d]{8,}', mystring.decode('utf-8'))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading