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
  • 16,542
  • 9
  • 83
  • 127
Andrei
  • 1,235
  • 4
  • 16
  • 33

0 Answers0