Skip to content

Instantly share code, notes, and snippets.

@nothrem
Created July 20, 2016 10:57
Show Gist options
  • Save nothrem/91ab1ac8738d44a7fa45fafbd4cb1f22 to your computer and use it in GitHub Desktop.
Save nothrem/91ab1ac8738d44a7fa45fafbd4cb1f22 to your computer and use it in GitHub Desktop.
if (!String.prototype.toNumberString) {
/**
* Converts each character into its numerical representation of given base
* By default converts to Hexadecimal (base = 16)
* This conversion is required for JWT implementation http://kjur.github.io/jsrsasign/
*
* Examples:
* '*123'.toNumberString(); //returns '2a313233'
* '*123'.toNumberString(10); //returns '42495051'
* '*123'.toNumberString(8); //returns '52616263';
* '*123'.toNumberString(16, 1, 2, 'Hex:'); //returns 'Hex:3132';
*/
String.prototype.toNumberString = function(base, start, length, prefix) {
start = start || 0;
length = start + (undefined === length ? this.length : length);
prefix = prefix || '';
for (; start < length; start++) {
prefix += this.charCodeAt(start).toString(base || 16);
}
return prefix;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment