Encodes a given input text string to a Base64 encoded string. :param input_string: str - The string to be encoded. :param exclude_padding: bool - Whether to exclude the padding characters '=' from the output. :return: str - The Base64 encoded string.
(input_string, exclude_padding=False)
| 4 | |
| 5 | |
| 6 | def encode_text_to_base64(input_string, exclude_padding=False): |
| 7 | """ |
| 8 | Encodes a given input text string to a Base64 encoded string. |
| 9 | |
| 10 | :param input_string: str - The string to be encoded. |
| 11 | :param exclude_padding: bool - Whether to exclude the padding characters '=' from the output. |
| 12 | :return: str - The Base64 encoded string. |
| 13 | """ |
| 14 | |
| 15 | # Convert the input string to bytes |
| 16 | input_bytes = input_string.encode("utf-8") |
| 17 | |
| 18 | # Encode these bytes and then decode the result to get a string |
| 19 | base64_bytes = base64.b64encode(input_bytes) |
| 20 | base64_string = base64_bytes.decode("utf-8") |
| 21 | |
| 22 | # Optionally exclude the padding characters |
| 23 | if exclude_padding: |
| 24 | base64_string = base64_string.rstrip("=") |
| 25 | |
| 26 | return base64_string |
| 27 | |
| 28 | |
| 29 | def decode_base64_to_text(base64_string): |
no test coverage detected