-
-
Save bytespider/1007834 to your computer and use it in GitHub Desktop.
Revisions
-
Rob Griffiths revised this gist
Jul 11, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,6 +5,6 @@ "keywords": [ "cryptography", "utf8" ] } -
Rob Griffiths revised this gist
Jun 4, 2011 . 3 changed files with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,8 +3,8 @@ function stringToByteArray(a,b,c,d,e,f,g,h,i){ for( b = [ e = d = 0 ] // initialise variables ; c = a.charCodeAt(d++) // get the character code from the string This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1 @@ function(a,b,c,d,e,f,g,h,i){for(b=[e=d=0];c=a.charCodeAt(d++);){g=128;c<g?b[e]=c:c<2048?f=1:c<65536?f=2:c<2<<20?f=3:0;for(h=e++,i=f;f-- >0;b[h]=g+(2<<6-i)+(c>>i*6))b[e++]=g+(c>>f*6&63)}return b} This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,7 @@ return bytes; } function stringToByteArray(a,b,c,d,e,f,g,h,i){for(b=[e=d=0];c=a.charCodeAt(d++);){g=128;c<g?b[e]=c:c<2048?f=1:c<65536?f=2:c<2<<20?f=3:0;for(h=e++,i=f;f-- >0;b[h]=g+(2<<6-i)+(c>>i*6))b[e++]=g+(c>>f*6&63)}return b}; document.getElementById( "ret" ).innerHTML = stringToByteArrayOld("hello☺䭢 it works") document.getElementById( "ret2" ).innerHTML = stringToByteArray("hello☺䭢 it works") -
Rob Griffiths revised this gist
Jun 4, 2011 . 3 changed files with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,8 +3,8 @@ function stringToByteArray(a,b,c,d,e,f,g,h,i){ for( b = [ e = 0 //d = 0 ] // initialise variables ; c = a.charCodeAt(d++) // get the character code from the string @@ -35,8 +35,8 @@ function stringToByteArray(a,b,c,d,e,f,g,h,i){ ; f-- > 0 // -1 and check if greater than 0 still ; b[h] = g + (2<<(6-i)) + (c >> i*6) // move onto the next slot in the byte array ) b[e++] = g + (c >> f*6 & 63) // shift f * 6 bits, mask 1byte and add 128 } return b // return the byte array This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1 @@ function(a,b,c,d,e,f,g,h,i){for(b=[e=0];c=a.charCodeAt(d++);){g=128;c<g?b[e]=c:c<2048?f=1:c<65536?f=2:c<2<<20?f=3:0;for(h=e++,i=f;f-- >0;b[h]=g+(2<<6-i)+(c>>i*6))b[e++]=g+(c>>f*6&63)}return b} This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,7 @@ return bytes; } function stringToByteArray(a,b,c,d,e,f,g,h,i){for(b=[e=0];c=a.charCodeAt(d++);){g=128;c<g?b[e]=c:c<2048?f=1:c<65536?f=2:c<2<<20?f=3:0;for(h=e++,i=f;f-- >0;b[h]=g+(2<<6-i)+(c>>i*6))b[e++]=g+(c>>f*6&63)}return b}; document.getElementById( "ret" ).innerHTML = stringToByteArrayOld("hello☺䭢 it works") document.getElementById( "ret2" ).innerHTML = stringToByteArray("hello☺䭢 it works") -
Rob Griffiths revised this gist
Jun 4, 2011 . 3 changed files with 6 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,9 +23,11 @@ function stringToByteArray(a,b,c,d,e,f,g,h,i){ ? f = 2 // number of bytes left to process : c < 2<<20 ? // finally, under 2097152 is 4bytes f = 3 // 3bytes left to process : 0 ; for( // process the remaining bytes indicated by `f` h = e++, This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1 @@ function(a,b,c,d,e,f,g,h,i){for(b=[e=d=0];c=a.charCodeAt(d++);){g=128;c<g?b[e]=c:c<2048?f=1:c<65536?f=2:c<2<<20?f=3:0;for(h=e++,i=f;f-- >0;)b[h]=g+(2<<6-i)+(c>>i*6),b[e++]=g+(c>>f*6&63)}return b} This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,7 @@ return bytes; } function stringToByteArray(a,b,c,d,e,f,g,h,i){for(b=[e=d=0];c=a.charCodeAt(d++);){g=128;c<g?b[e]=c:c<2048?f=1:c<g*512?f=2:c<2<<20?f=3:0;for(h=e++,i=f;f-- >0;)b[h]=g+(2<<6-i)+(c>>i*6),b[e++]=g+(c>>f*6&63)}return b}; document.getElementById( "ret" ).innerHTML = stringToByteArrayOld("hello☺䭢 it works") document.getElementById( "ret2" ).innerHTML = stringToByteArray("hello☺䭢 it works") -
Rob Griffiths revised this gist
Jun 4, 2011 . 3 changed files with 8 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,6 @@ function stringToByteArray(a,b,c,d,e,f,g,h,i){ for( b = [ e = d = 0 ] // initialise variables ; @@ -16,26 +15,26 @@ function stringToByteArray(a,b,c,d,e,f,g,h,i){ ? b[e] = c // add to byte array : c < 2048 // under 2048 2bytes ? f = 1 // number of bytes left to process : c < 65536 // under 65536 is 3bytes ? f = 2 // number of bytes left to process : c < 2<<20 && ( // finally, under 2097152 is 4bytes f = 3 // 3bytes left to process ); for( // process the remaining bytes indicated by `f` h = e++, i = f ; f-- > 0 // -1 and check if greater than 0 still ; ) b[h] = g + (2<<(6-i)) + (c >> i*6), // move onto the next slot in the byte array b[e++] = g + (c >> f*6 & 63) // shift f * 6 bits, mask 1byte and add 128 } return b // return the byte array This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1 @@ function(a,b,c,d,e,f,g,h,i){for(b=[e=d=0];c=a.charCodeAt(d++);){g=128;c<g?b[e]=c:c<2048?f=1:c<65536?f=2:c<2<<20&&(f=3);for(h=e++,i=f;f-- >0;)b[h]=g+(2<<6-i)+(c>>i*6),b[e++]=g+(c>>f*6&63)}return b} This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,8 +26,8 @@ return bytes; } function stringToByteArray(a,b,c,d,e,f,g,h,i){for(b=[e=d=0];c=a.charCodeAt(d++);){g=128;c<g?b[e]=c:c<2048?f=1:c<65536?f=2:c<2<<20&&(f=3);for(h=e++,i=f;f-- >0;)b[h]=g+(2<<6-i)+(c>>i*6),b[e++]=g+(c>>f*6&63)}return b}; document.getElementById( "ret" ).innerHTML = stringToByteArrayOld("hello☺䭢 it works") document.getElementById( "ret2" ).innerHTML = stringToByteArray("hello☺䭢 it works") </script> -
Rob Griffiths revised this gist
Jun 4, 2011 . 3 changed files with 17 additions and 20 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,40 +6,37 @@ function stringToByteArray(a,b,c,d,e,f,g,h,i){ e = f = d = 0 ] // initialise variables ; c = a.charCodeAt(d++) // get the character code from the string ; ){ g = 128; c < g // under 128 is UTF-8 (ASCII range), 1 byte ? b[e] = c // add to byte array : c < g*4 // under 2048 2bytes ? f = 1 // number of bytes left to process : c < 65536 // under 65536 is 3bytes ? f = 2 // number of bytes left to process : c < 2<<20 && ( // finally, under 2097152 is 4bytes f = 3 // 3bytes left to process ); for( // process the remaining bytes indicated by `f` h = e++, // move onto the next slot in the byte array i = f ; f-- > 0 // -1 and check if greater than 0 still ; b[h] = (2<<(5-f)) + (c >> 6*i) // add the first byte ) b[e++] = g + (c >> f*6 & 63) // shift f * 6 bits, mask 1byte and add 128 } return b // return the byte array } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1 @@ function(a,b,c,d,e,f,g,h,i){for(b=[e=f=d=0];c=a.charCodeAt(d++);){g=128,c<g?b[e]=c:c<g*4?f=1:c<65536?f=2:c<2<<20&&(f=3);for(h=e++,i=f;f-->0;b[h]=(2<<5-f)+(c>>6*i))b[e++]=g+(c>>f*6&63)}return b} This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ <!DOCTYPE html> <title>stringToByteArray</title> <div>Expected value: <b id="ret"></b></div> <div>Actual value: <b id="ret2"></b></div> <script> // write a small example that shows off the API for your example // and tests it in one fell swoop. @@ -26,7 +26,7 @@ return bytes; } var stringToByteArray = function(a,b,c,d,e,f,g,h,i){for(b=[e=f=d=0];c=a.charCodeAt(d++);){g=128,c<g?b[e]=c:c<g*4?f=1:c<65536?f=2:c<2<<20&&(f=3);for(h=e++,i=f;f-->0;b[h]=(2<<5-f)+(c>>6*i))b[e++]=g+(c>>f*6&63)}return b}; document.getElementById( "ret" ).innerHTML = stringToByteArray("hello☺䭢 it works") document.getElementById( "ret2" ).innerHTML = stringToByteArray("hello☺䭢 it works") -
Rob Griffiths revised this gist
Jun 4, 2011 . 2 changed files with 3 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -25,21 +25,17 @@ function stringToByteArray(a,b,c,d,e,f,g,h,i){ : c < 65536 // under 65536 is 3bytes ? f = 2 // number of bytes left to process : c < 2<<20 && ( // finally, under 2097152 is 4bytes f = 3 // 3bytes left to process ); for( // process the remaining bytes indicated by `f` h = e++, // move onto the next slot in the byte array i = f ; f-- > 0 // -1 and check if greater than 0 still ; ) b[e++] = g + (c >> f*6 & 63), // shift f * 6 bits, mask 1byte and add 128 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1 @@ function(a,b,c,d,e,f,g,h,i){for(b=[e=f=d=0];c=a.charCodeAt(d++);){g=128,c<g?b[e]=c:c<g*4?f=1:c<65536?f=2:c<2<<20&&(f=3);for(h=e++,i=f;f-->0;)b[e++]=g+(c>>f*6&63),b[h]=(2<<5-f)+(c>>6*i)}return b} -
Rob Griffiths revised this gist
Jun 4, 2011 . 3 changed files with 40 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ // http://en.wikipedia.org/wiki/UTF-8 function stringToByteArray(a,b,c,d,e,f,g,h,i){ for( b = [ e = @@ -11,36 +11,39 @@ function stringToByteArray(a,b,c,d,e,f){ c = a.charCodeAt(d++) // get the character code from the string ; ){ g = 128; c < g // under 128 is UTF-8 (ASCII range), 1 byte ? b[e] = c // add to byte array : c < g*4 // under 2048 2bytes ? ( //b[e] = 192 + (c >> 6), // shift down 6bits, add 192 to put it in the right range f = 1 // number of bytes left to process ) : c < 65536 // under 65536 is 3bytes ? ( //b[e] = 224 + (c >> 12), // shift down 12bits and put it into the correct range f = 2 // number of bytes left to process ) : c <2<<20 && ( // finally, under 2097152 is 4bytes //b[e] = 240 + (c >> 18), // shift down 18bits and 240 so its in the right range f = 3 // 3bytes left to process ); for( // process the remaining bytes indicated by `f` h = e++, // move onto the next slot in the byte array i = f ; f-- > 0 // -1 and check if greater than 0 still ; ) b[e++] = g + (c >> f*6 & 63), // shift f * 6 bits, mask 1byte and add 128 b[h] = (2<<(5-f)) + (c >> 6*i) } return b // return the byte array } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1 @@ function(a,b,c,d,e,f,g,h,i){for(b=[e=f=d=0];c=a.charCodeAt(d++);){g=128,c<g?b[e]=c:c<g*4?f=1:c<65536?f=2:c<2<<20&&(f=3);for(h=e++,i=f;f-->0;)b[e++]=g+(c>>f*6&63),b[h]=(2<<5-f)+(c>>6*i)}return b} This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,33 @@ <!DOCTYPE html> <title>stringToByteArray</title> <div>Expected value: <b id="ret"></b></div> <div>Actual value: <b id="ret2"></b></div> <script> // write a small example that shows off the API for your example // and tests it in one fell swoop. function stringToByteArrayOld(str) { var bytes = [], code, i; for(i = 0; i < str.length; i++) { code = str.charCodeAt(i); if (code < 128) { bytes.push(code); } else if (code < 2048) { bytes.push(192+(code>>6), 128+(code&63)); } else if (code < 65536) { bytes.push(224+(code>>12), 128+((code>>6)&63), 128+(code&63)); } else if (code < 2097152) { bytes.push(240+(code>>18), 128+((code>>12)&63), 128+((code>>6)&63), 128+(code&63)); } } return bytes; } var stringToByteArray = function(a,b,c,d,e,f,g,h,i){for(b=[e=f=d=0];c=a.charCodeAt(d++);){g=128,c<g?b[e]=c:c<g*4?f=1:c<65536?f=2:c<2<<20&&(f=3);for(h=e++,i=f;f-->0;)b[e++]=g+(c>>f*6&63),b[h]=(2<<5-f)+(c>>6*i)}return b}; document.getElementById( "ret" ).innerHTML = stringToByteArray("hello☺䭢 it works") document.getElementById( "ret2" ).innerHTML = stringToByteArray("hello☺䭢 it works") </script> -
Rob Griffiths revised this gist
Jun 4, 2011 . 3 changed files with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,22 +22,22 @@ function stringToByteArray(a,b,c,d,e,f){ f = 1 // number of bytes left to process ) : c < 65536 // under 65536 is 3bytes ? ( b[e] = 224 + (c >> 12), // shift down 12bits and put it into the correct range f = 2 // number of bytes left to process ) : c < 2<<20 && ( // finally, under 2097152 is 4bytes b[e] = 240 + (c >> 18), // shift down 18bits and 240 so its in the right range f = 3 // 3bytes left to process ); for( // process the remaining bytes indicated by `f` e++ // move onto the next slot in the byte array ; f-- > 0 // -1 and check if greater than 0 still ; ) b[e++] = 128 + (c >> f*6 & 63) // shift f * 6 bits, mask 1byte and add 128 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1 @@ function(a,b,c,d,e,f){for(b=[e=f=d=0];c=a.charCodeAt(d++);){c<128?b[e]=c:c<2048?(b[e]=192+(c>>6),f=1):c<65536?(b[e]=224+(c>>12),f=2):c<2<<20&&(b[e]=240+(c>>18),f=3);for(e++;f-->0;)b[e++]=128+(c>>f*6&63)}return b} This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ // write a small example that shows off the API for your example // and tests it in one fell swoop. var stringToByteArray = function(a,b,c,d,e,f){for(b=[e=f=d=0];c=a.charCodeAt(d++);){c<128?b[e]=c:c<2048?(b[e]=192+(c>>6),f=1):c<65536?(b[e]=224+(c>>12),f=2):c<2<<20&&(b[e]=240+(c>>18),f=3);for(e++;f-->0;)b[e++]=128+(c>>f*6&63)}return b}; document.getElementById( "ret" ).innerHTML = stringToByteArray("hello☺䭢it works👍") </script> -
Rob Griffiths revised this gist
Jun 4, 2011 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ <!DOCTYPE html> <title>stringToByteArray</title> <div>Expected value: <b>104, 101, 108, 108, 111, 226, 152, 186, 228, 173, 162, 105, 116, 32, 119, 111, 114, 107, 115, 237, 160, 189, 237, 177, 141</b></div> <div>Actual value: <b id="ret"></b></div> <script> // write a small example that shows off the API for your example -
Rob Griffiths revised this gist
Jun 4, 2011 . 1 changed file with 19 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ function stringToByteArray(str) { var bytes = [], code, i; for(i = 0; i < str.length; i++) { code = str.charCodeAt(i); if (code < 128) { bytes.push(code); } else if (code < 2048) { bytes.push(192+(code>>6), 128+(code&63)); } else if (code < 65536) { bytes.push(224+(code>>12), 128+((code>>6)&63), 128+(code&63)); } else if (code < 2097152) { bytes.push(240+(code>>18), 128+((code>>12)&63), 128+((code>>6)&63), 128+(code&63)); } } return bytes; } -
Rob Griffiths revised this gist
Jun 4, 2011 . 2 changed files with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,12 +32,12 @@ function stringToByteArray(a,b,c,d,e,f){ c < 2^21 && ( // finally, under 2097152 is 4bytes b[e] = 240 + (c >> 18), // shift down 18bits and 240 so its in the right range f = 3 // 3bytes left to process ); for( // process the remaining bytes indicated by `f` e++ // move onto the next slot in the byte array ; f-- // -1 and check if greater than 0 still ; ) b[e++] = 128 + (c >> f*6 & 63) // shift f * 6 bits, mask 1byte and add 128 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1 @@ function(a,b,c,d,e,f){for(b=[e=f=d=0];c=a.charCodeAt(d++);){c<128?b[e]=c:c<2048?(b[e]=192+(c>>6),f=1):c<2^16?(b[e]=224+(c>>12),f=2):c<2^21&&(b[e]=240+(c>>18),f=3);for(e++;f--;)b[e++]=128+(c>>f*6&63)}return b} -
Rob Griffiths revised this gist
Jun 4, 2011 . 5 changed files with 52 additions and 51 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,32 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,46 @@ // http://en.wikipedia.org/wiki/UTF-8 function stringToByteArray(a,b,c,d,e,f){ for( b = [ e = f = d = 0 ] // initialise variables ; c = a.charCodeAt(d++) // get the character code from the string ; ){ c < 128 // under 128 is UTF-8 (ASCII range), 1 byte ? b[e] = c // add to byte array : c < 2048 // under 2048 2bytes ? ( b[e] = 192 + (c >> 6), // shift down 6bits, add 192 to put it in the right range f = 1 // number of bytes left to process ) : c < 2^16 // under 65536 is 3bytes ? ( b[e] = 224 + (c >> 12), // shift down 12bits and put it into the correct range f = 2 // number of bytes left to process ) : c < 2^21 && ( // finally, under 2097152 is 4bytes b[e] = 240 + (c >> 18), // shift down 18bits and 240 so its in the right range f = 3 // 3bytes left to process ) for( // process the remaining bytes indicated by `f` e++ // move onto the next slot in the byte array ; f-- > 0 // -1 and check if greater than 0 still ; ) b[e++] = 128 + (c >> f*6 & 63) // shift f * 6 bits, mask 1byte and add 128 } return b // return the byte array } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1 @@ function(a,b,c,d,e,f){for(b=[e=f=d=0];c=a.charCodeAt(d++);){c<128?b[e]=c:c<2048?(b[e]=192+(c>>6),f=1):c<2^16?(b[e]=224+(c>>12),f=2):c<2^21&&(b[e]=240+(c>>18),f=3);for(e++;f-->0;)b[e++]=128+(c>>f*6&63)}return b} This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,10 @@ { "name": "stringToByteArray", "description": "Convert a string of characters to an array of UTF-8 bytes", "keywords": [ "cryptography", "utf8", ] } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ // write a small example that shows off the API for your example // and tests it in one fell swoop. var stringToByteArray = function(a,b,c,d,e,f){for(b=[e=f=d=0];c=a.charCodeAt(d++);){c<128?b[e]=c:c<2048?(b[e]=192+(c>>6),f=1):c<2^16?(b[e]=224+(c>>12),f=2):c<2^21&&(b[e]=240+(c>>18),f=3);for(e++;f-->0;)b[e++]=128+(c>>f*6&63)}return b}; document.getElementById( "ret" ).innerHTML = stringToByteArray("hello☺䭢it works👍") </script> -
140bytes revised this gist
Jun 1, 2011 . 1 changed file with 5 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,14 +1,12 @@ <!DOCTYPE html> <title>Foo</title> <div>Expected value: <b>undefined</b></div> <div>Actual value: <b id="ret"></b></div> <script> // write a small example that shows off the API for your example // and tests it in one fell swoop. var myFunction = function(){ /* the code here should be identical to the entry. */ } document.getElementById( "ret" ).innerHTML = myFunction() </script> -
140bytes revised this gist
Jun 1, 2011 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ <!DOCTYPE html> <body> <div>Expected value: <b>undefined</b></div> <div>Actual value: <b id="ret"></b></div> -
140bytes revised this gist
May 31, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,5 +9,5 @@ var myFunction = function(){ /* the code here should be identical to the entry. */ } var ret = myFunction() document.getElementById( "ret" ).innerHTML = ret </script> -
140bytes revised this gist
May 31, 2011 . 1 changed file with 13 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ <body> <div>Expected value: <b>undefined</b></div> <div>Actual value: <b id="ret"></b></div> </body> <script> // write a small example that shows off the API for your example // and tests it in one fell swoop. var myFunction = function(){ /* the code here should be identical to the entry. */ } var ret = myFunction() document.getElementById( "ret" ) = ret </script> -
140bytes revised this gist
May 31, 2011 . 3 changed files with 17 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,10 @@ function(){ // make sure // to annotate // your code // so everyone // can learn // from it! // see jed's entries // for examples. } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1 @@ function(){/* Your entry, a useful, unique, and valid JavaScript expression that packs as much functionality into 140 bytes as possible. */} This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,13 @@ { "name": "theNameOfYourLibWhichMustBeAValidCamelCasedJavaScriptIdentifier", "description": "This should be a short description of your entry.", "keywords": [ "five", "descriptive", "keywords", "or", "fewer" ] } -
140bytes revised this gist
May 26, 2011 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,9 @@ function(){ /* Rules: (1) anonymous function or IIFE // make sure (2) may execute immediately // to annotate (3) <=140 bytes // your code (4) no globals // so everyone (5) permissive license // can learn (6) have a good time! // from it! */} -
140bytes revised this gist
May 25, 2011 . 3 changed files with 8 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -16,7 +16,7 @@ Rules ----- All entries must exist in an `index.js` file, whose contents are 1. an assignable, valid Javascript expression that 2. contains no more than 140 bytes, and 3. does not leak to the global scope. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,9 @@ function(){ /* Rules: (1) anonymous function // make sure (2) may execute immediately // to annotate (3) <=140 bytes // your code (4) no globals // so everyone (5) permissive license // can learn (6) have a good time! // from it! */} This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1 @@ function(){/******************************************************************************************************************************/} -
140bytes revised this gist
May 23, 2011 . 2 changed files with 14 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,20 +1,13 @@ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE> Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. You just DO WHAT THE FUCK YOU WANT TO. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,7 +20,7 @@ All entries must exist in an `index.js` file, whose contents are 2. contains no more than 140 bytes, and 3. does not leak to the global scope. All entries must also be licensed under the [WTFPL](http://sam.zoy.org/wtfpl/) or equally permissive license. For more information -------------------- -
140bytes revised this gist
May 23, 2011 . 1 changed file with 3 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -16,12 +16,11 @@ Rules ----- All entries must exist in an `index.js` file, whose contents are 1. an assignable, valid Javascript expression, that 2. contains no more than 140 bytes, and 3. does not leak to the global scope. All entries must also be licensed under a license as or more permitting than the MIT license. For more information -------------------- -
140bytes revised this gist
May 22, 2011 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,6 +10,8 @@ How to play 2. Modify all the files to according to the rules below. 3. Save your entry and tweet it up! Keep in mind that thanks to the awesome sensibilities of the GitHub team, gists are just repos. So feel free to clone yours and work locally for a more comfortable environment, and to allow commit messages. Rules ----- All entries must exist in an `index.js` file, whose contents are -
140bytes revised this gist
May 21, 2011 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,4 +26,6 @@ For more information The [140byt.es](http://140byt.es) site hasn't launched yet, but for now follow [@140bytes](http://twitter.com/140bytes) on Twitter. To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to [the wiki](https://github.com/jed/140bytes/wiki/Byte-saving-techniques). 140byt.es is brought to you by [Jed Schmidt](http://jed.is). It was inspired by work from [Thomas Fuchs](http://mir.aculo.us) and [Dustin Diaz](http://www.dustindiaz.com/). -
140bytes revised this gist
May 18, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ // [OPTIONAL] A description of your library, phrased as a verb predicate. // The gist description is used by default. "description": "Explain the 140byt.es rules.", // [OPTIONAL] Up to 5 keywords used for indexing. "keywords": [ -
140bytes revised this gist
May 18, 2011 . 1 changed file with 13 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,16 @@ { // [REQUIRED] A name for your library. // This must match /^[a-z_]\w*$/i "name": "140bytes", // [OPTIONAL] A description of your library, phrased as a verb predicate. // The gist description is used by default. "description": "Explain the 140byt.es rules." // [OPTIONAL] Up to 5 keywords used for indexing. "keywords": [ "140bytes", "master", "rules" ] } -
140bytes revised this gist
May 18, 2011 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,4 +24,6 @@ All entries must also be licensed under the MIT license. For more information -------------------- The [140byt.es](http://140byt.es) site hasn't launched yet, but for now follow [@140bytes](http://twitter.com/140bytes) on Twitter. 140byt.es is brought to you by [Jed Schmidt](http://jed.is). It was inspired by work from [Thomas Fuchs](http://mir.aculo.us) and [Dustin Diaz](http://www.dustindiaz.com/). -
140bytes revised this gist
May 18, 2011 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,8 +14,8 @@ Rules ----- All entries must exist in an `index.js` file, whose contents are 1. a valid Javascript function expression, that 2. optionally self-executes, 2. contains no more than 140 bytes, and 3. does not pollute global scope. -
140bytes revised this gist
May 18, 2011 . 1 changed file with 12 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,9 +7,20 @@ How to play ----------- 1. Click the  button above to fork this gist. 2. Modify all the files to according to the rules below. 3. Save your entry and tweet it up! Rules ----- All entries must exist in an `index.js` file, whose contents are 1. a valid Javascript function expression, that is 2. is optionally self-executing, 2. contains no more than 140 bytes, and 3. does not pollute global scope. All entries must also be licensed under the MIT license. For more information -------------------- -
140bytes revised this gist
May 18, 2011 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ 140byt.es ========= A tweet-sized, fork-to-play, community-curated collection of JavaScript. How to play ----------- @@ -12,4 +12,5 @@ How to play For more information -------------------- The [140byt.es](http://140byt.es) site hasn't launched yet, but for now follow [@140bytes](http://twitter.com/140bytes) on Twitter.
NewerOlder