ChemPal Documentation - v1.6.0
    Preparing search index...

    Function base64EncodeUtf8

    • Base64-encodes a UTF-8 string (raw bytes, not URI-encoded). ASCII-only input uses btoa directly.

      Parameters

      • str: string

        String to encode

      Returns string

      Base64 representation

      base64EncodeUtf8("hello"); // "aGVsbG8="
      base64EncodeUtf8("098f6bcd4621d373cade4e832627b4f6"); // MD5 hex digest
      export function base64EncodeUtf8(str: string): string {
      // Intentionally matches the ASCII control range (0x00–0x7f) to detect pure-ASCII input.
      // eslint-disable-next-line no-control-regex
      if (/^[\x00-\x7f]*$/.test(str)) {
      return btoa(str);
      }

      const bytes = new TextEncoder().encode(str);
      let binary = '';
      for (let i = 0; i < bytes.length; i++) {
      binary += String.fromCharCode(bytes[i]);
      }
      return btoa(binary);
      }