0

Is there a built-in Python function that convert from digit number to hex string with zero leading digit?

I wrote her:

def convert_to_hex_string(number):
    number = format(number, 'x')
    if len(number) == 1:
        number = '0' + number
    return number

>>> convert_to_hex_string(7)
'07'
>>> convert_to_hex_string(10)
'0a'
>>> convert_to_hex_string(11)
'0b'
>>> convert_to_hex_string(666)
'29a'

but I was wondering could already have a built-in in Python (analogue .toString("X2") in C#)

Delgan
  • 18,571
  • 11
  • 90
  • 141
Andrei
  • 1,313
  • 4
  • 18
  • 35

0 Answers0