1

I have a code (python) that converts any character on a keyboard to binary, but if the character has 2 zeros in the beginning of the binary code, it deletes 1 of the zeros, so it can't be converted back.

<body>
  <p>Click to open <a href="localhost:8000" target="_blank" rel="noopener noreferrer">local host</a>.</p>
  <div>Type here to convert characters to binary;</div>
  <input type="text" id="test-input"/>
  <button id="submit-button" type="submit" pys-onClick="my_function">OK</button>
  <p>Text: <div id="test-output"></div></p>
  <p>Binary: <div id="test-output2"></div></p>

    <py-script>
        from js import console

        def my_function(*args, **kwargs):

            #print('args:', args)
            #print('kwargs:', kwargs)

            console.log(f'args: {args}')
            console.log(f'kwargs: {kwargs}')
    
            text = Element('test-input').element.value

            #print('text:', text)
            console.log(f'text: {text}')
            textTwo = text.replace(" ", "_").replace("@", "_").replace("#", "_").replace("$", "_").replace("%", "_").replace("^", "_").replace("&", "_").replace("*", "_").replace("(", "_").replace(")", "_").replace("-", "_").replace("+", "_").replace("=", "_").replace("{", "_").replace("}", "_").replace("[", "_").replace("]", "_").replace("|", "_").replace(":", "_").replace(";", "_").replace('"', "_").replace("<", "_").replace("/", "_").replace("!", "_").replace("'", "_").replace("?", "_").replace(",", "_").replace(".", "_").replace(">", "_")

            output = ' '.join(map(bin,bytearray(textTwo,'ascii')))
            new_output = output.replace("b", "")
            Element('test-output').element.innerText = textTwo
            Element('test-output2').element.innerText = new_output
    </py-script>
  <br>
  <div>Type here to convert binary to characters;</div>
  <input type="text" id="test-inputs"/>
  <button id="submit-button22" type="submit" pys-onClick="my_functions">OK</button>
  <p>Binary: <div id="test-outputs"></div></p>
  <p>Text: <div id="test-outputs2"></div></p>
    <py-script>
        from js import console
        import re

        def my_functions(*args, **kwargs):

            #print('args:', args)
            #print('kwargs:', kwargs)

            console.log(f'args: {args}')
            console.log(f'kwargs: {kwargs}')
    
            text = Element('test-inputs').element.value

            #print('text:', text)
            console.log(f'text: {text}')
            newName = re.sub('[\\\\qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM23456789]', '', text)
            new_text = newName.replace(" ", "")
            bins = new_text
            binc = [bins[i:i + 8] for i in range(0, len(bins), 8)]
            nums = [int(chunk, 2) for chunk in binc]
            output = ''.join(chr(num) for num in nums)
            Element('test-outputs').element.innerText = text
            Element('test-outputs2').element.innerText = output
    </py-script>
</body>

Everything converts nicely, but numbers don't. I fixed it so all marks like !@#$%^&*() and spaces are put as _.

Edit: Here is the part that needs fixing;

from js import console

def my_function(*args, **kwargs):

    #print('args:', args)
    #print('kwargs:', kwargs)

    console.log(f'args: {args}')
    console.log(f'kwargs: {kwargs}')
    
    text = Element('test-input').element.value

    #print('text:', text)
    console.log(f'text: {text}')
    textTwo = text.replace(" ", "_").replace("@", "_").replace("#", "_").replace("$", "_").replace("%", "_").replace("^", "_").replace("&", "_").replace("*", "_").replace("(", "_").replace(")", "_").replace("-", "_").replace("+", "_").replace("=", "_").replace("{", "_").replace("}", "_").replace("[", "_").replace("]", "_").replace("|", "_").replace(":", "_").replace(";", "_").replace('"', "_").replace("<", "_").replace("/", "_").replace("!", "_").replace("'", "_").replace("?", "_").replace(",", "_").replace(".", "_").replace(">", "_")

    output = ' '.join(map(bin,bytearray(textTwo,'ascii')))
    new_output = output.replace("b", "")
    Element('test-output').element.innerText = textTwo
    Element('test-output2').element.innerText = new_output
7
  • 2
    Could you include just the code without a surrounding HTML document? Commented Feb 14, 2024 at 22:08
  • Could you add examples of sample input, the expected output, and the output you're getting instead? Commented Feb 14, 2024 at 22:15
  • Please make a minimal reproducible example with just the Python functions taking text without the HTML with sample inputs and expected vs. actual outputs. Commented Feb 14, 2024 at 22:15
  • Use a single call to re.sub() instead of dozens of .replace() calls to remove all the punctuation. Commented Feb 14, 2024 at 22:16
  • The input is: kyle 3. The sample output is: 01101011 01111001 01101100 01100101 01011111 0110011. The expected output for the number 3 is: 00110011. When there are 2 zeros at the beginning of a binary (like the 3) it prints without the 2nd zero (0110011 instead of 00110011). Commented Feb 14, 2024 at 22:23

1 Answer 1

0

output = ' '.join(map(bin,bytearray(textTwo,'ascii'))) doesn't make sure the binary is 8 digits long, use:

output = ' '.join([format(n,'08b') for n in textTwo.encode('ascii')])

(and you won't need new_output line).

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that worked. It is a school project, and that line that you changed was the line the teacher wanted me to use, but it doesn't work as shown. Thank you for fixing that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.