Class Index | File Index

Classes


Namespace encoding

Encoding functions
Defined in: encoding.js.

Namespace Summary
Constructor Attributes Constructor Name and Description
 

Support for ASCII, UTF-8, Base64 and Hex encoding.

Field Summary
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 Summary
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
Namespace Detail
encoding

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.
Field Detail
<static> encoding.ascii
The ASCII alphabet, can be used directly

<static> encoding.ascii_table
ASCII code table. Has its own dynamic accessor.

<static> encoding.base64
Base64 alphabet. Is missing the last two characters to support URL style

<static> encoding.hex
Hex alphabet.

<static> encoding.utf8
UTF-8 alphabet. Initially contains the null byte, actual value is in utf8.js
Method Detail
<private> <static> {number} encoding._searchCharTable(char, alphabet)
Binary search of a character inside a sorted string.
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.

<static> {number} encoding.a2b(input)
ASCII code of input character.
Parameters:
{string} input
character
Returns:
{number} ASCII code of input. Unlike encoding.charCode, will always return 0 if input is non-ASCII.

<static> {string} encoding.astr2hstr(input)
Convert an ASCII string to an hexadecimal string
Parameters:
{string} input
must be ASCII (uses a2b internally)
Returns:
{string} hex representation of input

<static> {string} encoding.b2a(input)
ASCII character from its code.
Parameters:
{number} input
ASCII code, only first 8 bits are taken into account.
Returns:
{string} associated ASCII character.

<static> {string} encoding.b2h(input)
Hex representation of a byte.
Parameters:
{number} input
byte
Returns:
{string} hex representation of the input, has always length 2.

<static> {number} encoding.b64c(input)
The code of a character in the base64 alphabet. Accept +- or /_, fallback is 0.
Parameters:
{string} input
base64 character
Returns:
{number} base64 code from 0 to 63

<static> {string} encoding.base64_decode(input)
Decode a base64-encoded string to ASCII
Parameters:
{string} input
base64 (can be url-safe or not)
Returns:
{string} the decoded ASCII string.

<static> {string} encoding.base64_encode(input)
Encode an ASCII string to base64
Parameters:
{string} input
ASCII
Returns:
{string} base64 encoding of input.

<static> {string} encoding.base64_urlencode(input)
Encode an ASCII string to url-compatible base64
Parameters:
{string} input
ASCII
Returns:
{string} url-base64 encoding of input.

<static> {number} encoding.charCode(input)
charCodeAt emulation. Returns the code point of the input character
Parameters:
{string} input
character
Returns:
{number} 16 bit character point. If input if not ASCII and utf8.js is not loaded, will return 0.

<static> {string} encoding.fromCharCode(input)
fromCharCode emulation. Can create unicode characters.
Parameters:
{number} input
code point (truncated to 16 bits)
Returns:
{string} UCS-2 character of requested code point.

<static> {string} encoding.hstr2astr(input)
Convert an hexadecimal string to ASCII.
Parameters:
{string} input
hex string
Returns:
{string} ASCII equivalent

<static> {string} encoding.utf8_decode(input)
Decode an UTF-8 string to its raw ASCII equivalent.
Parameters:
{string} input
JavaScript string (containing unicode characters)
Returns:
{string} decoded ASCII string

<static> {string} encoding.utf8_encode(input)
Encode a raw ASCII string back into a JavaScript string
Parameters:
{string} input
ASCII
Returns:
{string} JavaScript unicode string representing the UTF-8 input.

Documentation generated by JsDoc Toolkit on Mon, 26 Nov 2012 17:47:46 UTC