Base64 Encoding in Scilab

Scilab provides built-in support for base64 encoding/decoding through its base64encode and base64decode functions.

// Here's the string we'll encode/decode.
data = 'abc123!?$*&()''"-=@~';

// Scilab supports standard base64 encoding.
// Here's how to encode using the standard encoder.
sEnc = base64encode(data);
disp(sEnc);

// Decoding is straightforward in Scilab.
sDec = base64decode(sEnc);
disp(string(sDec));
disp(' ');

// Scilab doesn't have a built-in URL-compatible base64 encoding.
// However, we can create a simple function to achieve this:
function str = urlsafe_b64encode(input)
    str = strsubst(base64encode(input), '+', '-');
    str = strsubst(str, '/', '_');
endfunction

function str = urlsafe_b64decode(input)
    str = strsubst(input, '-', '+');
    str = strsubst(str, '_', '/');
    str = base64decode(str);
endfunction

// Now we can use these functions for URL-safe encoding/decoding
uEnc = urlsafe_b64encode(data);
disp(uEnc);
uDec = urlsafe_b64decode(uEnc);
disp(string(uDec));

The string encodes to slightly different values with the standard and URL-safe base64 encoders (trailing + vs -) but they both decode to the original string as desired.

To run this Scilab script, save it to a file (e.g., base64_encoding.sce) and execute it in Scilab:

--> exec('base64_encoding.sce', -1)
YWJjMTIzIT8kKiYoKSciLT1Afg==
abc123!?$*&()''"-=@~
 
YWJjMTIzIT8kKiYoKSciLT1Afg==
abc123!?$*&()''"-=@~

Note that Scilab’s base64 functions work with ASCII strings by default. For handling non-ASCII characters or binary data, you might need to use additional string manipulation or binary data handling functions.