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
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).