├── textbin ├── __init__.py └── textbin.py ├── CONTRIBUTING.md ├── .vscode └── settings.json ├── LICENSE └── README.md /textbin/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # contributions 2 | your contributions will be highly appreciated. I hope it be of help to you thank you. c-o-m-o-n 3 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "[python]": { 3 | "editor.defaultFormatter": "ms-python.black-formatter" 4 | }, 5 | "python.formatting.provider": "none" 6 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright © 2022 Collins Omondi 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # textbin 2 | 3 | **textbin** is a Python package that provides text-to-binary and binary-to-text conversion, as well as JSON-to-base64 and base64-to-JSON encoding and decoding. 4 | 5 | ## Installation 6 | 7 | You can install **textbin** using pip from PyPI: 8 | 9 | ```bash 10 | pip install textbin 11 | ``` 12 | 13 | Alternatively, you can find the project on GitHub: 14 | 15 | [GitHub Repository](https://github.com/C-o-m-o-n/textbin) 16 | 17 | ## Usage 18 | 19 | ### Text and Binary 20 | 21 | ```python 22 | from textbin.textbin import * 23 | 24 | # Convert text to binary 25 | word = "hello" 26 | converted_word = textbin.to_binary(word) 27 | print(converted_word) # Output: '1101000 1100101 1101100 1101100 1101111' 28 | 29 | # Convert binary to text 30 | binary = "1101000 1100101 1101100 1101100 1101111" 31 | converted_binary = textbin.to_text(binary) 32 | print(converted_binary) # Output: hello 33 | ``` 34 | 35 | ### JSON and Base64 36 | 37 | ```python 38 | from textbin.textbin import json_to_base64, base64_to_json 39 | 40 | # Encode a JSON object to base64 41 | word = {"foo": "bar"} 42 | converted_word = json_to_base64(word) 43 | print(converted_word) # Output: eyJmb28iOiAiYmFyIn0= 44 | 45 | # Decode a base64 string to a JSON object 46 | base64_string = "eyJmb28iOiAiYmFyIn0=" 47 | converted_binary = base64_to_json(base64_string) 48 | print(converted_binary) # Output: {'foo': 'bar'} 49 | ``` 50 | 51 | ## Contributions 52 | 53 | Contributions to **textbin** are welcome! You can find the project's GitHub repository and contribute to its development. 54 | 55 | - [GitHub Repository](https://github.com/C-o-m-o-n/textbin) 56 | 57 | ## Contributors 58 | 59 | - [Roldex](https://github.com/r0ld3x) 60 | 61 | Your contributions are highly appreciated! We hope that **textbin** proves helpful to you. Thank you for using it. 62 | 63 | #### I hope it be of help to you thank you. c-o-m-o-n 64 | -------------------------------------------------------------------------------- /textbin/textbin.py: -------------------------------------------------------------------------------- 1 | import json 2 | import base64 3 | from typing import Union 4 | 5 | class Textbin: 6 | def to_binary(self, text: str) -> str: 7 | """Convert text to binary representation.""" 8 | binary = " ".join(format(ord(i), "b") for i in str(text)) 9 | return binary 10 | 11 | def to_text(self, binary: str) -> str: 12 | """Convert binary representation to text.""" 13 | text = "".join(chr(int(i, 2)) for i in binary.split()) 14 | return text 15 | 16 | def json_to_base64(self, json_data: Union[dict, list]) -> str: 17 | """Convert a JSON object to base64-encoded string.""" 18 | try: 19 | data = json.dumps(json_data) 20 | base64_encoded = base64.b64encode(data.encode()).decode() 21 | return base64_encoded 22 | except json.JSONDecodeError as e: 23 | raise ValueError(f"Invalid JSON data: {e}") 24 | except Exception as e: 25 | raise ValueError(f"Error encoding to base64: {e}") 26 | 27 | def base64_to_json(self, base64_string: str) -> Union[dict, list]: 28 | """Convert a base64-encoded string to a JSON object.""" 29 | try: 30 | base64_decode = base64.b64decode(base64_string).decode() 31 | json_data = json.loads(base64_decode) 32 | return json_data 33 | except base64.binascii.Error as e: 34 | raise ValueError(f"Invalid base64 data: {e}") 35 | except json.JSONDecodeError as e: 36 | raise ValueError(f"Invalid JSON data: {e}") 37 | except Exception as e: 38 | raise ValueError(f"Error decoding from base64: {e}") 39 | 40 | if __name__ == "__main__": 41 | textbin = Textbin() 42 | 43 | word = {"foo": "bar"} 44 | 45 | try: 46 | converted_word = textbin.json_to_base64(word) 47 | print(converted_word) # Output: eyJmb28iOiAiYmFyIn0= 48 | 49 | base64_string = "eyJmb28iOiAiYmFyIn0=" 50 | converted_binary = textbin.base64_to_json(base64_string) 51 | print(converted_binary) # Output: {'foo': 'bar'} 52 | except ValueError as e: 53 | print(f"Error: {e}") 54 | --------------------------------------------------------------------------------