Home > Net >  Extract hex number from string python
Extract hex number from string python

Time:12-26

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:

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

The output i'm looking for is :

0637064406280020063A064A0631002006450648062C0648062F000A002D0020002D0020002D000A00300030003A0627064406420627062606450629000A0030003A0631062C06480639

CodePudding user response:

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'))
  • Related