<< Click to Display Table of Contents >> Navigation: 3. Script Language > AI - Artificial Intelligence Commands > AIC. - Artificial Intelligence Command > AI - Internet Commands > AI - Internet Commands |
MiniRobotLanguage (MRL)
Encode to Base64
Encode Data using BASE64
Intention
Base64 is a method for encoding 8-bit binary data (such as executable programs, ZIP files, or images) into a string consisting only of readable,
codepage-independent ASCII characters. It is used in the Internet standard Multipurpose Internet Mail Extensions (MIME) and is used there for sending email attachments.
This is necessary to ensure the smooth transport of any binary data, as SMTP in its original version was not designed to handle binary data.
Base64 is often used in JSON to encode binary data, such as images or other files, that cannot be directly represented in JSON.
JSON is a text-based format, so binary data must be encoded into a text format before it can be included in a JSON object.
Base64 is one of the most common methods for doing this.
The binary data (for example images generated using AI) is first converted into a Base64 string, which can then be included in the JSON object as a normal string value.
This allows the binary data to be transmitted and stored along with the rest of the JSON data.
Base64 and Base64url are both ways to encode binary data in string form.
The problem with Base64 is that it contains the characters `+`, `/`, and `=`, which have a reserved meaning in some filesystem names and URLs.
So Base64url solves this by replacing `+` with `-` and `/` with `_`.
The trailing padding character `=` can be eliminated when not needed, but in a URL it would instead most likely be `%` URL encoded.
Then the encoded data can be included in a URL without problems¹.
So, if you need to transmit or save Base64-encoded text where `+`, `/`, or `=` have special meaning, e.g. in URLs where all 3 do, then it is better to use Base64url.
AIC.Decode from BASE64 will convert either Base6 or Base64URL back to Binary Data.
AIC.Encode to BASE64 will convert Binary Data to MIME/BASE64 data.
It is adviced to save binary data to a file and prevent it from going through unnecessary Variable resolutions.
The command itself looks like this:
' $$SRC - MIME-Format
' $$TAR - Binary or Text-Data Result
AIC.DECODE FROM BASE64|$$SRC|$$TAR
VAR.$$SRC=What happens under BASE64 with the + and / Characters?
VAR.$$SRC+$$SRC$$SRC$$SRC$$SRC$$SRC
LEN.$$SRC|$$LEN
PRT.----------------------
PRT.Original:
PRT.($$LEN)->$$SRC
AIC.Encode to BASE64|$$SRC
LEN.$$SRC|$$LEN
PRT.----------------------
PRT.ENCODED:
PRT.($$LEN)->$$SRC
PRT.----------------------
AIC.Decode from BASE64|$$SRC
LEN.$$SRC|$$LEN
PRT.DECODED:
PRT.($$LEN)->$$SRC
PRT.----------------------
MBX.It works!
ENR.
The Result looks somehow like you see below.
As you can see the MIME encoded Binary is 432 bytes long, while the original size was 324 bytes. That is generally 134% the size of the original size.
If the MIME-Text contains linefeeds ($crlf$), these will be ignored.
This is the revers command to AIC.Encode to Base64.
Syntax
AIC.Encode to BASE64|P1[|P2]
Parameter Explanation
P1 - source variable. If P2 is missing then its also the destination variable
P2 - (optional) destination variable. If omitted the result is placed in the variable in P1.
Example
'*************************************
' AIC.Encode to BASE64 and
' AIC.Decode from BASE64
' using one Input-Output-Variable
'*************************************
VAR.$$SRC=What happens under BASE64 with the + and / Characters?
VAR.$$SRC+$$SRC$$SRC$$SRC$$SRC$$SRC
LEN.$$SRC|$$LEN
PRT.----------------------
PRT.Original:
PRT.($$LEN)->$$SRC
AIC.Encode to BASE64|$$SRC
LEN.$$SRC|$$LEN
PRT.----------------------
PRT.ENCODED:
PRT.($$LEN)->$$SRC
PRT.----------------------
AIC.Decode from BASE64|$$SRC
LEN.$$SRC|$$LEN
PRT.DECODED:
PRT.($$LEN)->$$SRC
PRT.----------------------
MBX.It works!
ENR.
Remarks
-
Limitations:
-
See also:
• 1.5.2 Working with Text-Strings
• VAR. - Variable Set Value/Clear
• IVS. / NVS. - If-Variable-String