How i convert number HEX with base 1 (0x1) to base 2 (0x01).
Hex_B1 = 0x1 Hex_B2 = 0x01
hex(int('110', 2)) = 0x06
How i convert number HEX with base 1 (0x1) to base 2 (0x01).
Hex_B1 = 0x1 Hex_B2 = 0x01
hex(int('110', 2)) = 0x06
Because hex
produces a string, you can just parse the string after conversion to hex.
leading_zeros = 2
str_hex = hex(int('110', 2))
final_hex = '0x'+str_hex[2:].zfill(leading_zeros)
As pointed out by Martijn Pieters in the comments, the much better solution is to use Pythons built in format
function.
format(5, '#04x')