Sure! Below is a simple Bit Calculator Program written in Python that:
✅ Takes user input as text
✅ Converts the text to its binary (bit) representation
✅ Allows the user to modify the binary
✅ Converts the modified binary back to text
✅ Bit Calculator Program (Text ↔ Binary Converter)
python
def text_to_bits(text):
"""Converts text to binary string (8 bits per character)."""
return ''.join(format(ord(char), '08b') for char in text)
def bits_to_text(bits):
"""Converts binary string back to text."""
try:
# Ensure bits length is multiple of 8
if len(bits) % 8 != 0:
raise ValueError("Binary length must be a multiple of 8.")
chars = [bits[i:i+8] for i in range(0, len(bits), 8)]
return ''.join(chr(int(char, 2)) for char in chars)
except ValueError as e:
return f"Error: {e}"
def main():
print("🔤 Bit Calculator - Convert Text ⇄ Binary")
print("----------------------------------------")
while True:
print("\nOptions:")
print("1. Enter text to convert to binary")
print("2. Enter binary to convert to text")
print("3. Exit")
choice = input("Choose an option (1/2/3): ").strip()
if choice == '1':
text = input("Enter your text: ")
binary = text_to_bits(text)
print(f"\nBinary representation:\n{binary}")
elif choice == '2':
binary = input("Enter binary (8-bit groups, e.g., 01001000): ").strip()
text = bits_to_text(binary)
print(f"\nConverted text:\n{text}")
elif choice == '3':
print("👋 Goodbye!")
break
else:
print("❌ Invalid option. Please choose 1, 2 or 3.")
if __name__ == "__main__":
main()
🔧 Features:
Converts any text to binary (0s and 1s).
Lets you paste raw binary and get original text back.
Validates binary input (must be multiple of 8 bits).
🧪 Example:
code
Enter your text: Hi
Binary representation:
0100100001101001
Then enter that binary to get back "Hi".
Would you like a GUI version (with Tkinter) or web-based version (with Flask or HTML/JS)?
User
can you make a html page with input and ouput result instead to install python and all the dependencies or create a .bat files who can handle this
Assistant
Absolutely! 👍 Here's how you can run a Bit Calculator directly in your browser using a simple HTML + JavaScript page — no Python, no installations needed!
✅ Option 1: HTML Page (Runs in Browser)
This HTML file includes:
Input field for text → converts to binary
Input field for binary → converts to text
All logic runs client-side in your browser
💾 Save As: bit-calculator.html
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bit Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
max-width: 600px;
margin: auto;
}
textarea {
width: 100%;
height: 100px;
margin-bottom: 10px;
}
button {
margin-top: 10px;
padding: 8px 16px;
}
label {
font-weight: bold;
}
</style>
</head>
<body>
<h1>🔤 Bit Calculator</h1>
<div>
<label for="inputText">Enter Text:</label><br>
<textarea id="inputText" placeholder="Type your text here..."></textarea><br>
<button onclick="textToBinary()">Convert to Binary</button>
</div>
<div>
<label for="binaryOutput">Binary Result:</label><br>
<textarea id="binaryOutput" readonly></textarea>
</div>
<hr>
<div>
<label for="inputBinary">Enter Binary (8-bit chunks):</label><br>
<textarea id="inputBinary" placeholder="e.g. 0100100001101001"></textarea><br>
<button onclick="binaryToText()">Convert to Text</button>
</div>
<div>
<label for="textOutput">Text Result:</label><br>
<textarea id="textOutput" readonly></textarea>
</div>
<script>
function textToBinary() {
const text = document.getElementById('inputText').value;
let binary = '';
for (let i = 0; i < text.length; i++) {
const bits = text.charCodeAt(i).toString(2).padStart(8, '0');
binary += bits;
}
document.getElementById('binaryOutput').value = binary;
}
function binaryToText() {
const binary = document.getElementById('inputBinary').value;
// Validate binary input: must be multiple of 8
if (binary.length % 8 !== 0) {
alert("⚠️ Binary input must have a length that's a multiple of 8.");
return;
}
let result = '';
for (let i = 0; i < binary.length; i += 8) {
const byte = binary.slice(i, i + 8);
const charCode = parseInt(byte, 2);
result += String.fromCharCode(charCode);
}
document.getElementById('textOutput').value = result;
}
</script>
</body>
</html>
▶️ How to Use:
Copy the code above.
Paste into any text editor (Notepad, VS Code, etc).
Save as bit-calculator.html.
Open it in any modern browser (Chrome, Edge, Firefox…).
✅ Option 2: Windows .bat File (Alternative)
If you really want a .bat file that opens the HTML in your default browser:
💾 Save As: run-bit-calculator.bat
bat
@echo off
start "" "%~dp0bit-calculator.html"
⚠️ You must place both run-bit-calculator.bat and bit-calculator.html in the same folder.
Double-click the .bat file, and it will open the HTML calculator in your browser.
Would you like me to add copy-to-clipboard buttons or color themes too? 😊