Base64 Encoder

***

Encodes a string to Base64 format, handling Unicode characters using TextEncoder.

Loading...
Loading...
Settings

Description

Convert text to Base64 format using different output options. Ideal for encoding data as plain text, data URIs, or HTML hyperlinks. For decode use Base64 Decoder.

How to Use

  1. Enter Your Text: Type or paste the text you want to encode into the editor.
  2. Choose Output Format: Select 'Plain text', 'Data URI', or 'HTML Hyperlink' to define how the encoded text should be formatted.
  3. Set Line Separator: Opt for 'LF (Unix)' or 'CRLF (Windows)' to specify how new lines should be handled.
  4. Multiple Entries Option: Toggle 'Multiple entries' if you want to encode each line of your input text separately.

More details

The TextEncoder interface accepts a sequence of code points as input and produces a stream of UTF-8 bytes as output.

/**
 * ๐Ÿช„ Qit.tools
 * https://qit.tools
 *
 * Decodes a Base64 encoded string back to normal string, handling Unicode with TextDecoder.
 * Includes error handling for invalid Base64 encoding.
 *
 * @param {string} input - The Base64 encoded string to decode.
 * @param {string} [label] - Optional label for TextDecoder.
 * @param {TextDecoderOptions} [options] - Options for TextDecoder.
 * @returns {string} - The decoded string.
 */
export function decodeBase64(
  input: string,
  label: string = "utf-8",
  options?: TextDecoderOptions,
): string {
  if (typeof input !== "string") {
    throw new Error("Input must be a string.");
  }
  try {
    const binaryString = atob(input);
    const bytes = new Uint8Array(
      Array.from(binaryString, (char) => char.charCodeAt(0)),
    );
    const decoder = new TextDecoder(label, options);
    return decoder.decode(bytes);
  } catch (error) {
    throw new Error("Failed to decode the input string from Base64.");
  }
}