-1

Hi i would like to add leading zeros into binary to make it to have 8 digits. I've tried using zfill() but it doesnt seem to work and i have no idea how to use format() cause all the answers i've found are all keeping the 0b which i dont want that.

Here's what i've tried:

lenToBin = bin(payloadLength).replace("0b", "")
        if payloadLength == 30 or payloadLength == 31:
            binResult = lenToBin.zfill(3)
        else:
            binResult = lenToBin.zfill(2)
John
  • 435
  • 1
  • 5
  • 11

1 Answers1

-1

Is this what you want:

lenToBin = bin(payloadLength).replace("0b", "").zfill(8)
James
  • 32,991
  • 4
  • 47
  • 70