Skip to content

Instantly share code, notes, and snippets.

@timothyarmstrong
Created May 15, 2011 01:11
Show Gist options
  • Save timothyarmstrong/972798 to your computer and use it in GitHub Desktop.
Save timothyarmstrong/972798 to your computer and use it in GitHub Desktop.
Convert a string to a function call that puts together that string form character codes
/**
* Convert a string to a function call that puts together that string form character codes.
* Useful when you are trying to avoid using quotation characters in JavaScript (because they
* are being escaped) but you still want to use a string.
*
* @author Timothy Armstrong
* @param str String The string you want to convert
* @return String The JavaScript code that can replace your string
*/
function toChars(str) {
var out = 'String.fromCharCode(';
for (var i = 0; i < str.length; i++) { out += str.charCodeAt(i) + ',' }
return out.substr(0,out.length-1) + ')';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment