Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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