Skip to content

Instantly share code, notes, and snippets.

@mrdoob
Created November 6, 2010 06:29
Show Gist options
  • Save mrdoob/665235 to your computer and use it in GitHub Desktop.
Save mrdoob/665235 to your computer and use it in GitHub Desktop.
Simple encode/decode for Array of Numbers (6-bit)
function decode( string ) {
var output = [];
string.split('').forEach( function ( v ) { output.push( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf( v ) ); } );
return output;
}
function encode( array ) {
var output = "";
array.forEach( function ( v ) { output += "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt( v ); } );
return output;
}
var data = [20, 20, 20, 0, 18, 18, 10, 2, 15, 30, 10, 5, 20, 20, 20, 1, 18, 18, 10, 2, 15, 30, 10, 10];
var encoded = encode( data );
var decoded = decode( encoded );
console.log( data );
console.log( encoded );
console.log( decoded );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment