Decorating Hex function to pad zeros

前端 未结 6 1889
无人共我
无人共我 2020-11-29 01:31

I wrote this simple function:

def padded_hex(i, l):
    given_int = i
    given_len = l

    hex_result = hex(given_int)[2:] # remove \'0x\' from beginning o         


        
6条回答
  •  醉话见心
    2020-11-29 02:00

    What I needed was

    "{:02x}".format(2)   # '02'
    "{:02x}".format(42)  # '2a'
    

    or as f-strings:

    f"{2:02x}"   # '02'
    f"{42:02x}"  # '2a'
    

提交回复
热议问题