Skip to content

Commit def19a1

Browse files
committed
fix: return 0x0 from octal_to_hex("0")
The loop builds no hex digit for 0, so the function returned '0x'. The check in the module's own __main__ block expects '0x' + hex(int(num, 8))[2:].upper(), which is '0x0'.
1 parent 6e03a48 commit def19a1

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

‎conversions/octal_to_hexadecimal.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ def octal_to_hex(octal: str) -> str:
77
'0x40'
88
>>> octal_to_hex("235")
99
'0x9D'
10+
>>> octal_to_hex("0")
11+
'0x0'
1012
>>> octal_to_hex(17)
1113
Traceback (most recent call last):
1214
...
@@ -42,7 +44,7 @@ def octal_to_hex(octal: str) -> str:
4244
revhex += hex_char[decimal & 15]
4345
decimal >>= 4
4446

45-
return "0x" + revhex[::-1]
47+
return "0x" + (revhex[::-1] or "0")
4648

4749

4850
if __name__ == "__main__":

0 commit comments

Comments
 (0)