0

Hi let's say I have 5 decimal numbers.How can I convert them firstly to hexadecimal and to string afterwards. Is there any built in function for this? I tried below

numbers = [123,46,0,11,6]
for number in numbers:
    print(hex(number).lstrip("0x").upper())

Expected output is

7B
2E
00
0B
06

but the result is

7B
2E

B
6
  • 2
    `strip` will remove all characters inside the string you give it as an argument. So `"0x00".strip("0x")` will leave you an empty string. Just slice the string like `hex(number)[2:]` – isaactfa May 08 '19 at 19:52

1 Answers1

0

Add zfill:

numbers = [123,46,0,11,6]
for number in numbers:
    print(hex(number).lstrip("0x").zfill(2).upper())
Michael H.
  • 3,323
  • 2
  • 23
  • 31