Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Print decimal 0 as 000 in python

i am converting hex data to decimal and the range is from 00 to FF

 hex_data = "FF"
int("0x" + hex_data , 16) 

returns 255 but when i give 0 as hexdata it gives 0 wheras i need it as 000

how to do it

like image 679
Ragav Avatar asked Feb 19 '23 05:02

Ragav


1 Answers

You need to format it:

hex_data = "FF"
number = int("0x" + hex_data, 16) 
print '%03d' % number # either this
print '{:03d}'.format(number) # or this (Python >= 2.6)
like image 139
ThiefMaster Avatar answered Feb 21 '23 20:02

ThiefMaster