Wednesday, 22 February 2023

convert bytes to bits with leading zeros

I know that i can do this :

byte = 58

format ( byte , '08b' )


>>> '00111010'

with two bytes i have to do

format( bytes , '016b')

but if i doesn't have the number of bytes i can't set a number for format so i have to do :

with open('file','rb')as a:
    b = a.read()
    c = int.from_bytes ( b )
    d = format( c ,'b')
d = (8-len(a)%8)*'0'+d

but i was wondering if there was easier way to do this and i want this without using any loops

thanks!



from convert bytes to bits with leading zeros

No comments:

Post a Comment