Spaces:
Sleeping
Sleeping
Create binary_utils.py
Browse files- binary_utils.py +10 -0
binary_utils.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def binary_to_text(binary_data):
|
2 |
+
# Converts binary (e.g., 0s and 1s) into ASCII text
|
3 |
+
try:
|
4 |
+
n = int(binary_data, 2)
|
5 |
+
return n.to_bytes((n.bit_length() + 7) // 8, 'big').decode()
|
6 |
+
except Exception:
|
7 |
+
return "Invalid binary input"
|
8 |
+
|
9 |
+
def text_to_binary(text):
|
10 |
+
return ' '.join(format(ord(c), '08b') for c in text)
|