Base64Decode
→ stringDecodes a standard Base64-encoded string. Optionally specify the character set. Decodes any valid standard Base64 string, not only values produced by Base64Encode().
Runtime verified
Differs from official docs
Test scripts included
Syntax
Platform.Function.Base64Decode(encodedString[, charset])
1–2 arguments
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
encodedString |
string | Yes | Base64 encoded string to decode |
charset |
string | No | Character set to use when decoding, such as ASCII or UTF-8 |
Show test script
<script runat="server">
Platform.Load("core", "1.1.5");
/*
* Chapter: Parameters — Platform.Function.Base64Decode(encodedString[, charset])
*
* Proves:
* 1. The member exists and is invocable with 1 argument (a successful call
* is the only reliable existence proof for a Platform.Function member).
* 2. encodedString is REQUIRED: the 0-argument form throws.
* 3. charset is OPTIONAL: the 1-argument and the 2-argument forms both
* succeed and agree for pure-ASCII input.
* 4. max_args is 2: a 3-argument call throws.
* 5. The documented charset values "ASCII" and "UTF-8" are both accepted.
* 6. return_type is string: typeof the result is "string" for both arities.
*
* EXPECTED OUTPUT: every line starts with PASS. A FAIL means the runtime no
* longer matches the documented claim and the page must be revised.
*/
function assert(id, actual, expected) {
Platform.Response.Write((actual === expected ? "PASS " : "FAIL ") + id + " -> [" + actual + "]\n");
}
function assertThrows(id, fn) {
var threw = false;
try { fn(); } catch (ex) { threw = true; }
Platform.Response.Write((threw ? "PASS " : "FAIL ") + id + " -> " + (threw ? "threw" : "did NOT throw") + "\n");
}
var ENC = "SGVsbG8sIFdvcmxkIQ==";
/* 1 + 3. The 1-argument form works — existence proof. */
var oneArg = Platform.Function.Base64Decode(ENC);
assert("1-argument call decodes", oneArg, "Hello, World!");
/* 6. return_type is string. */
assert("typeof 1-argument result is string", String(typeof oneArg), "string");
/* 3 + 5. The optional charset argument is accepted. */
var utf8 = Platform.Function.Base64Decode(ENC, "UTF-8");
assert("charset 'UTF-8' decodes", utf8, "Hello, World!");
var ascii = Platform.Function.Base64Decode(ENC, "ASCII");
assert("charset 'ASCII' decodes", ascii, "Hello, World!");
assert("typeof 2-argument result is string", String(typeof utf8), "string");
/* 3. Both arities agree for pure-ASCII input. */
assert("1-argument and UTF-8 results agree for ASCII input", oneArg === utf8 ? "true" : "false", "true");
assert("UTF-8 and ASCII results agree for ASCII input", ascii === utf8 ? "true" : "false", "true");
/* 2. encodedString is required. */
assertThrows("arity 0 throws (encodedString is required)", function () {
return Platform.Function.Base64Decode();
});
/* 4. max_args is 2. */
assertThrows("arity 3 throws (max_args is 2)", function () {
return Platform.Function.Base64Decode(ENC, "UTF-8", "extra");
});
</script>
Description
Decodes a standard Base64-encoded string back to its original value. Use the optional charset parameter to control how the decoded bytes are interpreted.
It decodes any valid standard Base64 string — the input does not have to have been produced by Base64Encode().
Differs from official Salesforce docs
The official docs imply it only decodes values created by the matching Base64Encode() function, but the runtime decodes any valid standard Base64 string.
Show test script — decodes any standard Base64, not just Base64Encode output
<script runat="server">
Platform.Load("core", "1.1.5");
/*
* Differs-from-docs claim: the official Salesforce documentation describes
* Base64Decode as the counterpart of Base64Encode and implies it only
* decodes values that this function itself produced. At runtime it decodes
* ANY valid standard Base64 string, whatever produced it.
*
* Official docs: decodes values created by the matching Base64Encode()
* SFMC runtime: decodes any valid standard Base64 string
*
* Proves both halves of the claim:
* 1. DEV externally-produced standard Base64 literals — including all
* three padding forms (no padding, "=", "==") — decode correctly
* even though nothing in this session encoded them.
* 2. Base64Encode output still decodes, so the deviation is an EXTENSION
* of the documented behaviour, not a replacement for it.
* 3. The output of Base64Encode is itself standard Base64: encoding a
* known value yields the literal an external encoder produces.
*
* EXPECTED OUTPUT: every line starts with PASS.
*/
function assert(id, actual, expected) {
Platform.Response.Write((actual === expected ? "PASS " : "FAIL ") + id + " -> [" + actual + "]\n");
}
/* 1. DEVIATION — external literals, one per padding form. */
assert("DEV 'TWFu' (no padding) decodes to 'Man'", Platform.Function.Base64Decode("TWFu"), "Man");
assert("DEV 'TWE=' (one pad char) decodes to 'Ma'", Platform.Function.Base64Decode("TWE="), "Ma");
assert("DEV 'TQ==' (two pad chars) decodes to 'M'", Platform.Function.Base64Decode("TQ=="), "M");
assert("DEV a longer external literal decodes", Platform.Function.Base64Decode("VGhpcyB3YXMgYSBCYXNlNjQgZW5jb2RlZCBzdHJpbmcu"), "This was a Base64 encoded string.");
assert("DEV an external literal decodes with an explicit charset too", Platform.Function.Base64Decode("SGVsbG8sIFdvcmxkIQ==", "UTF-8"), "Hello, World!");
/* 2. The documented case still holds. */
var own = Platform.Function.Base64Encode("round trip");
assert("Base64Encode output still decodes", Platform.Function.Base64Decode(own), "round trip");
/* 3. Base64Encode emits standard Base64. */
assert("Base64Encode('Man') is the standard literal 'TWFu'", Platform.Function.Base64Encode("Man"), "TWFu");
assert("Base64Encode('M') is the standard literal 'TQ=='", Platform.Function.Base64Encode("M"), "TQ==");
</script>
For a single-argument form without charset control, see Base64Decode() under the Core Library bare-name functions.
Show test script
<script runat="server">
Platform.Load("core", "1.1.5");
/*
* Chapter: Description — decodes a standard Base64 string back to its
* original value, with the optional charset controlling how the decoded
* bytes are interpreted.
*
* Proves:
* 1. Base64Decode reverses Base64Encode — the round trip returns the
* original value for plain text, punctuation and digits.
* 2. The round trip also holds through the 2-argument charset form.
* 3. DEV the input does NOT have to come from Base64Encode: a Base64
* literal that this script never encoded decodes correctly
* (official docs imply only Base64Encode output is decodable).
* 4. The charset argument controls byte interpretation: a UTF-8 encoded
* multi-byte character round-trips through the "UTF-8" charset.
* 5. The bare-name Core Library form Base64Decode(encodedString)
* produces the same value as the 1-argument Platform.Function form.
* 6. Decoding the empty string yields the empty string.
*
* EXPECTED OUTPUT: every line starts with PASS.
*/
function assert(id, actual, expected) {
Platform.Response.Write((actual === expected ? "PASS " : "FAIL ") + id + " -> [" + actual + "]\n");
}
/* 1. Round trip through Base64Encode. */
var plain = "This was a Base64 encoded string.";
var enc = Platform.Function.Base64Encode(plain);
assert("Base64Encode produced the expected standard Base64", enc, "VGhpcyB3YXMgYSBCYXNlNjQgZW5jb2RlZCBzdHJpbmcu");
assert("round trip returns the original value", Platform.Function.Base64Decode(enc), plain);
var digits = "0123456789";
assert("round trip of digits", Platform.Function.Base64Decode(Platform.Function.Base64Encode(digits)), digits);
var punct = "a+b/c=d?e&f";
assert("round trip of punctuation", Platform.Function.Base64Decode(Platform.Function.Base64Encode(punct)), punct);
/* 2. Round trip through the charset form. */
var encUtf8 = Platform.Function.Base64Encode(plain, "UTF-8");
assert("round trip via the UTF-8 charset form", Platform.Function.Base64Decode(encUtf8, "UTF-8"), plain);
/* 3. DEVIATION — a literal this script never encoded decodes fine. */
assert("DEV decodes a Base64 literal never produced by Base64Encode (docs imply it cannot)", Platform.Function.Base64Decode("SGVsbG8="), "Hello");
/* 4. The charset controls byte interpretation for multi-byte characters. */
var eAcute = String.fromCharCode(233);
assert("UTF-8 charset decodes a multi-byte character", Platform.Function.Base64Decode("Y2Fmw6k=", "UTF-8"), "caf" + eAcute);
/* 5. The bare-name Core Library form agrees. */
assert("bare-name Base64Decode agrees with Platform.Function.Base64Decode", Base64Decode(enc), Platform.Function.Base64Decode(enc));
/* 6. The empty string decodes to the empty string. */
assert("empty input decodes to the empty string", Platform.Function.Base64Decode(""), "");
</script>
Example
var encodedStr = Platform.Function.Lookup("forBase64Info", "ReceiptData", "ReceiptKey", "stringValue");
var decodedStr = Platform.Function.Base64Decode(encodedStr);
Write(decodedStr);
Show test script
<script runat="server">
Platform.Load("core", "1.1.5");
/*
* Chapter: Example — read an encoded value, decode it, write the result.
*
* Proves the shape of the documented example:
* 1. A Base64 value held in a variable decodes into a plain string that
* Write() can output.
* 2. The decoded value is a real string, so string operations work on it.
* 3. Write() emits exactly the decoded value.
*
* NOT ASSERTED: the Platform.Function.Lookup("forBase64Info", ...) call in
* the page example. It depends on a data extension fixture that does not
* exist in the verification business unit, so the lookup itself is not
* deterministically observable here. The example's Base64Decode half — the
* part this page documents — is asserted below with the same value shape.
*
* EXPECTED OUTPUT: every line starts with PASS.
*/
function assert(id, actual, expected) {
Platform.Response.Write((actual === expected ? "PASS " : "FAIL ") + id + " -> [" + actual + "]\n");
}
/* 1. Stand-in for the Lookup result: an encoded value in a variable. */
var encodedStr = Platform.Function.Base64Encode("Receipt #4711");
var decodedStr = Platform.Function.Base64Decode(encodedStr);
assert("the decoded lookup value is the original string", decodedStr, "Receipt #4711");
/* 2. The result is a genuine string. */
assert("typeof decodedStr is string", String(typeof decodedStr), "string");
assert("string methods work on the decoded value", decodedStr.substring(0, 7), "Receipt");
assert("the decoded value has the expected length", String(decodedStr.length), "13");
/* 3. Write() emits exactly the decoded value. */
var out = Platform.Function.Base64Decode("V3JpdGUgbWU=");
assert("Write() input is the decoded value", out, "Write me");
Platform.Response.Write("PASS Write(decodedStr) emitted -> [" + out + "]\n");
</script>