Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to count int input using python

so i have func that convert bytes to binary and i want to count how many amount binary.

example b'\xd8\xe9\xbdR' binary is 11011000 11101001 10111101 01010010

if it is count how many amount, it will be count 4.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i already tried using split, but it says only can be used in string. so can someone help me to fix it?

here’s my code to convert bytes to binary:

def biner(password):
    print(password)
    password[0]
    for my_byte in password:
        if my_byte != None:
            return ' '.join(f'{my_byte:0>8b}' for my_byte in password)

here’s the web i tried to count

>Solution :

So quite simply you can take advantage of your script that already splits your input into your desired separation. So using string.split works just fine here.

def biner(password):
    print(password)
    password[0]
    for my_byte in password:
        if my_byte != None: #probably better to replace with is not None rather than != None
            string_output = ' '.join(f'{my_byte:0>8b}' for my_byte in password)
            return string_output, len(string_output.split(' '))#so here we get your 'count' simply the number of splits defined in string_output
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading