Namespace encoding
Encoding functions
Defined in: encoding.js.
Constructor Attributes | Constructor Name and Description |
---|---|
Support for ASCII, UTF-8, Base64 and Hex encoding. |
Field Attributes | Field Name and Description |
---|---|
<static> |
encoding.ascii
The ASCII alphabet, can be used directly
|
<static> |
encoding.ascii_table
ASCII code table.
|
<static> |
encoding.base64
Base64 alphabet.
|
<static> |
encoding.hex
Hex alphabet.
|
<static> |
encoding.utf8
UTF-8 alphabet.
|
Method Attributes | Method Name and Description |
---|---|
<private> <static> |
encoding._searchCharTable(char, alphabet)
Binary search of a character inside a sorted string.
|
<static> |
encoding.a2b(input)
ASCII code of input character.
|
<static> |
encoding.astr2hstr(input)
Convert an ASCII string to an hexadecimal string
|
<static> |
encoding.b2a(input)
ASCII character from its code.
|
<static> |
encoding.b2h(input)
Hex representation of a byte.
|
<static> |
encoding.b64c(input)
The code of a character in the base64 alphabet.
|
<static> |
encoding.base64_decode(input)
Decode a base64-encoded string to ASCII
|
<static> |
encoding.base64_encode(input)
Encode an ASCII string to base64
|
<static> |
encoding.base64_urlencode(input)
Encode an ASCII string to url-compatible base64
|
<static> |
encoding.charCode(input)
charCodeAt emulation.
|
<static> |
encoding.fromCharCode(input)
fromCharCode emulation.
|
<static> |
encoding.hstr2astr(input)
Convert an hexadecimal string to ASCII.
|
<static> |
encoding.utf8_decode(input)
Decode an UTF-8 string to its raw ASCII equivalent.
|
<static> |
encoding.utf8_encode(input)
Encode a raw ASCII string back into a JavaScript string
|
Support for ASCII, UTF-8, Base64 and Hex encoding.
DJS is simply not good at encoding, because it lacks the built-in functions to get the character code at a given offset from a string (charCodeAt), and its inverse, String.fromCharCode.
This means we have to use a literal object whose field names are ASCII characters and values are the associated codes, and a string containing every character sorted by code for the inverse operation.
For UTF-8, such a table would be too large and instead, we use binary search on the string containing all UTF-8 characters, using the built in lexicographic order of JavaScript. Since the complete UTF-8 alphabet is itself 200KB, it is loaded from its own file, utf8.js. Loading this file is optional: without it every non-ASCII character is treated like the null byte.
Author: Anonymized.
- Parameters:
- {string} char
- character to search
- {string} alphabet
- string whose characters are sorted in lexicographic order
- Returns:
- {number} the position where char occurs in alphabet, or 0 if not found.
- Parameters:
- {string} input
- character
- Returns:
- {number} ASCII code of input. Unlike encoding.charCode, will always return 0 if input is non-ASCII.
- Parameters:
- {string} input
- must be ASCII (uses a2b internally)
- Returns:
- {string} hex representation of input
- Parameters:
- {number} input
- ASCII code, only first 8 bits are taken into account.
- Returns:
- {string} associated ASCII character.
- Parameters:
- {number} input
- byte
- Returns:
- {string} hex representation of the input, has always length 2.
- Parameters:
- {string} input
- base64 character
- Returns:
- {number} base64 code from 0 to 63
- Parameters:
- {string} input
- base64 (can be url-safe or not)
- Returns:
- {string} the decoded ASCII string.
- Parameters:
- {string} input
- ASCII
- Returns:
- {string} base64 encoding of input.
- Parameters:
- {string} input
- ASCII
- Returns:
- {string} url-base64 encoding of input.
- Parameters:
- {string} input
- character
- Returns:
- {number} 16 bit character point. If input if not ASCII and utf8.js is not loaded, will return 0.
- Parameters:
- {number} input
- code point (truncated to 16 bits)
- Returns:
- {string} UCS-2 character of requested code point.
- Parameters:
- {string} input
- hex string
- Returns:
- {string} ASCII equivalent
- Parameters:
- {string} input
- JavaScript string (containing unicode characters)
- Returns:
- {string} decoded ASCII string
- Parameters:
- {string} input
- ASCII
- Returns:
- {string} JavaScript unicode string representing the UTF-8 input.