So I've got an algorithm that does this already, but the code is ugly and slow. I assume that there's a built-in or some function in the libs that give faster and more precise results.
What I want it where I can give a final number of bits, a string, list or deque (anything that can be cast to a string in the end).
func(totalBits, binaryInStringOrListForm) -> String or List with leading zeroes
val = deque(["1", "0"])
while len(val) < 8:
There is a built-in, the
str.ljust() method. It takes the target width and an optional fill character (defaulting to a space). You can use it to pad out a string with
val = val.ljust(8, '0')
There is also an equivalent
str.rjust() method to add padding on the left.
However, if you want leading zeros (as opposed to trailing), I'd use the
str.zfill() method as that takes any
+ prefix into account.