Monday, October 17, 2022

[FIXED] How to count int input using python

Issue

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.

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


Answered By - Jason Chia
Answer Checked By - Pedro (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.