Friday, April 15, 2011

Python one-liner to convert hex numbers to decimal

A one-line to convert all hex numbers from standard in to decimal:


cat log | \
python -c 'import sys,re;[sys.stdout.write(re.sub("0x([a-fA-F0-9]+)",lambda m : str(int(m.group(0),16)),l)) for l in sys.stdin]'