Unofficial LSL Reference

[[articles:base64]]


Unofficial LSL reference

User Tools

Login

You are currently not logged in! Enter your authentication credentials below to log in. You need to have cookies enabled to log in.

Login

Forgotten your password? Get a new one: Set new password

Base64

Base64 is one way of storing sequences of characters in strings, that represent a sequence of bytes. It's similar to hexadecimal, which provides 4 bits per character, and thus it needs 2 characters per byte. Base64 is more compact: with base64 only 4 characters are needed for every 3 bytes, or ~1.333 characters per byte on average.

The representation uses the characters A to Z, a to z, 0 to 9, the plus sign (+) and the slash (/), in that order. Each character represents a 6-bit binary number from 000000 to 111111, which is part of a byte. So A represents 000000, B 000001, ..., Z 011001, a 011010, ..., z 110011, 0 110100, ..., 9 111101, + represents 111110, and finally / represents 111111.

These sequences of six bits are parts of bytes. The first character represents the leftmost 6 bits of the first byte; the second character represents the rightmost two bits of the first byte, plus the leftmost 4 bits of the second byte; the third character represents the rightmost 4 bits of the second byte, plus the leftmost two bits of the third byte; the fourth character represents the rightmost 6 bits of the third byte, and then the cycle repeats.

So, every four Base64 characters represent three bytes. Let's see for example what sequence of bytes does the string "z9+A" represent. z is 110011, 9 is 111101, + is 111110 and A is 000000 so the bit string is:

  110011 111101 111110 000000

To see what bytes it represents, we take that bit string but placing the separating spaces differently:

  11001111 11011111 10000000

which is the byte sequence 0xCF, 0xDF, 0x80. Therefore we can say that the Base64 string "z9+A" represents the byte sequence 0xCF 0xDF 0x80.

The standard that specifies how Base64 is encoded requires that Base64 strings have a number of characters that is a multiple of four. When it is not, equal signs (=) are appended until it is. LSL is permissive with respect to padding, and accepts unpadded strings. When it generates Base64 strings, it generates them with padding.

This representation is useful in LSL because it's one way to store arbitrary sequences of bytes, while being able to manipulate them as strings. The following LSL functions deal with Base64 strings specifically: