Base64

class Base64 extends Object

Converts between binary data and Base64 text. Both methods are called on the class itself.

The class is exported by the KS module:

#Import "Ks" { Base64 }

Table of Contents

Encode

Encodes binary data as Base64 text.

Text := Base64.Encode(Value , Encoding := "UTF-8")

Parameters

Value

Type: Buffer, StringBuffer or String

The data to encode. A Buffer is encoded exactly as it stands, so Encoding does not apply to it. A StringBuffer is encoded as its text, the same as the equivalent string.

Encoding

Type: String

The encoding a string or StringBuffer Value is converted to bytes with, named as for FileEncoding. If omitted, it defaults to UTF-8. A name which cannot be resolved throws a ValueError.

Return Value

Type: String

Returns the Base64 text.

Decode

Decodes Base64 text to bytes.

Data := Base64.Decode(Text)

Parameters

Text

Type: String

The Base64 text to decode.

Return Value

Type: Buffer

Returns a Buffer holding the decoded bytes.

Errors

Text which is not well-formed Base64 throws a ValueError.

Examples

#Import "Ks" { Base64 }

Base64.Encode("Hello, world!")             ; "SGVsbG8sIHdvcmxkIQ=="
StrGet(Base64.Decode("SGVsbG8="), "UTF-8") ; "Hello"
Base64.Encode("abc", "UTF-16")             ; "YQBiAGMA"

Sends HTTP Basic authentication credentials:

#Import "Ks" { Http, Base64 }

Http.Get(Url, {Headers: Map("Authorization", "Basic " Base64.Encode(User ":" Password))})

Base32, Hex, Crypt, Buffer