Last active
August 29, 2015 14:24
-
-
Save ahvonenj/b0d657da2bea7e1355c6 to your computer and use it in GitHub Desktop.
Charcode helpers
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 characters
function toCharcodes(str) | |
{ | |
var codestring = ''; | |
var comma = ''; | |
for(var i = 0; i < str.length; i++) | |
{ | |
comma = (i === str.length-1) ? '' : ','; | |
codestring += str.charCodeAt(i) + comma; | |
} | |
return codestring; | |
} | |
function charcodesToString(charcodesAsStringOrArrayOrArguments) | |
{ | |
var codetype = null; | |
var returnstr = ''; | |
if(arguments.length > 1) | |
{ | |
codetype = 'arguments'; | |
} | |
else if(charcodesAsStringOrArrayOrArguments.constructor === Array) | |
{ | |
codetype = 'array'; | |
} | |
else if(charcodesAsStringOrArrayOrArguments.constructor === String) | |
{ | |
codetype = 'string'; | |
} | |
else | |
{ | |
codetype = 'unknown'; | |
console.log('Could not resolve charcodetype'); | |
return; | |
} | |
switch(codetype) | |
{ | |
case 'arguments': | |
for (var i = 0; i < arguments.length; i++) | |
{ | |
returnstr += String.fromCharCode(arguments[i]); | |
} | |
break; | |
case 'array': | |
for (var i = 0; i < charcodesAsStringOrArrayOrArguments.length; i++) | |
{ | |
returnstr += String.fromCharCode(charcodesAsStringOrArrayOrArguments[i]); | |
} | |
break; | |
case 'string': | |
var arr = charcodesAsStringOrArrayOrArguments.replace(/[[:space:]]/g, '').split(','); | |
for (var i = 0; i < arr.length; i++) | |
{ | |
returnstr += String.fromCharCode(arr[i]); | |
} | |
break; | |
default: | |
break; | |
} | |
return returnstr; | |
} | |
var c1 = toCharcodes('document.write("test1");'); | |
var c2 = toCharcodes('document.write("test2");'); | |
var c3 = toCharcodes('document.write("test3");'); | |
console.log(c1); | |
console.log(c2); | |
console.log(c3); | |
console.log(charcodesToString(c1.split(','))); | |
console.log(charcodesToString(100,111,99,117,109,101,110,46,119,114,105,116,101,40,34,116,101,115,116,50,34,41,59)); | |
console.log(charcodesToString(c3)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment