Skip to content

Commit 3fbc86b

Browse files
authored
Merge pull request #3 from r0ld3x/main
added json to base64 and base64 to json
2 parents 7e65d90 + 5abbbb4 commit 3fbc86b

File tree

3 files changed

+84
-14
lines changed

3 files changed

+84
-14
lines changed

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"[python]": {
3+
"editor.defaultFormatter": "ms-python.black-formatter"
4+
},
5+
"python.formatting.provider": "none"
6+
}

README.md

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
#### textbin is a python package that converts text to binary and binary to text
44

55
# installation
6+
67
> [pypi link](https://pypi.org/project/textbin/)
7-
>> `pip install textbin`
8+
>
9+
> > `pip install textbin`
810
911
> [git repo](https://github.com/C-o-m-o-n/textbin)
1012
11-
# usage
12-
``` python
13+
# usage
14+
15+
```python
1316
from textbin.textbin import *
1417

1518
word = "hello"
@@ -21,7 +24,7 @@ print(converted_word)
2124
$ '1101000 1100101 1101100 1101100 1101111'
2225

2326

24-
---
27+
---
2528

2629
binary = "1101000 1100101 1101100 1101100 1101111"
2730

@@ -30,8 +33,32 @@ converted_binary = textbin.to_binary(binary)
3033
print(converted_binary)
3134
$ hello
3235

36+
```
37+
38+
# JSON and Base64
39+
40+
```python
41+
from textbin.textbin import json_to_base64,base64_to_base64
42+
43+
word = {"foo": "bar"}
44+
45+
converted_word = textbin.json_to_base64(word)
46+
47+
print(converted_word) ## eyJmb28iOiAiYmFyIn0=
48+
49+
base64_string = "eyJmb28iOiAiYmFyIn0="
50+
51+
converted_binary = textbin.base64_to_base64(base64_string)
52+
53+
print(converted_binary) ## {'foo': 'bar'}
54+
55+
3356
```
3457

3558
# contributions
59+
60+
- [Roldex](https://github.com/r0ld3x)
61+
3662
### your contributions will be highly appreciated
37-
#### I hope it be of help to you thank you. c-o-m-o-n
63+
64+
#### I hope it be of help to you thank you. c-o-m-o-n

textbin/textbin.py

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,49 @@
1-
class Textbin():
2-
def to_binary(self, text):
3-
binary = " ".join(format(ord(i), 'b')for i in str(text))
4-
return binary
5-
6-
def to_text(self, binary):
7-
text = "".join(chr(int(i, 2)) for i in binary.split())
8-
return text
9-
1+
import json
2+
import base64
3+
4+
5+
class Textbin:
6+
def to_binary(self, text):
7+
binary = " ".join(format(ord(i), "b") for i in str(text))
8+
return binary
9+
10+
def to_text(self, binary):
11+
text = "".join(chr(int(i, 2)) for i in binary.split())
12+
return text
13+
14+
def json_to_base64(self, json_data: dict or list):
15+
try:
16+
data = json.dumps(json_data)
17+
except Exception as e:
18+
print("[ERROR] Invalid json")
19+
return False
20+
base64_encoded = base64.b64encode(data.encode()).decode()
21+
return base64_encoded
22+
23+
def base64_to_base64(self, base64_string: str):
24+
try:
25+
base64_decode = base64.b64decode(base64_string).decode()
26+
except Exception as e:
27+
print("[ERROR] Invalid json")
28+
return False
29+
json_data = json.loads(
30+
base64_decode,
31+
)
32+
return json_data
33+
34+
1035
textbin = Textbin()
36+
# print(textbin.json_to_base64({"hi": "hello"}))
37+
# print(textbin.base64_to_base64("eyJoaSI6ICJoZWxsbyJ9=="))
38+
39+
word = {"foo": "bar"}
40+
41+
converted_word = textbin.json_to_base64(word)
42+
43+
print(converted_word) ## eyJmb28iOiAiYmFyIn0=
44+
45+
base64_string = "eyJmb28iOiAiYmFyIn0="
1146

47+
converted_binary = textbin.base64_to_base64(base64_string)
1248

49+
print(converted_binary) ## {'foo': 'bar'}

0 commit comments

Comments
 (0)