4

If I have a random string and want to encode it into another string that only contains alphanumeric characters, what is the most efficient way to do it in JavaScript / NodeJS?

Obviously it must be possible to convert the output string back to the original input string when needed.

Thanks!

Pensierinmusica
  • 6,404
  • 9
  • 40
  • 58

2 Answers2

11

To encode to an alphanumeric string you should use an alphanumeric encoding. Some popular ones include hexadecimal (base16), base32, base36, base58 and base62. The alternatives to hexadecimal are used because the larger alphabet results in a shorter encoded string. Here's some info:

  • Hexadecimal is popular because it is very common.
  • Base32 and Base36 are useful for case-insensitive encodings. Base32 is more human readable because it removes some easy-to-misread letters. Base32 is used in gaming and for license keys.
  • Base58 and Base62 are useful for case-sensitive encodings. Base58 is also designed to be more human readable by removing some easy-to-misread letters. Base58 is used by Flickr, Bitcoin and others.

In NodeJS hexadecimal encoding is natively supported, and can be done as follows:

// Encode
var hex = new Buffer(string).toString('hex');

// Decode
var string = new Buffer(hex, 'hex').toString();

It's important to note that there are different implementations of some of these. For example, Flickr and Bitcoin use different implementations of Base58.

Grokify
  • 15,092
  • 6
  • 60
  • 81
  • Thanks @Grokify! What encoder among the ones you mention is the most performant (fastest) and is natively supported in JavaScript? – Pensierinmusica Aug 16 '15 at 16:54
  • Hexadecimal is typically the most performant since that is natively implemented. The JS implementation in the linked SO Answer looks very straight-forward so I would try that and see if it's fast enough for you. Base32/36/58/62 are nice when you want a short encoded string. – Grokify Aug 16 '15 at 17:13
  • Hi Grokify, when you say "hexadecimal is natively implemented", to what native JS methods are you precisely referring to for encoding into and decoding from hex? – Pensierinmusica Aug 16 '15 at 17:26
  • For JavaScript, you can use the native `Number.prototype.toString()` method and `parseInt()` function for hexadecimal and conversions up to Base36. The linked SO Answer demonstrates using these in small wrapper methods. Here are some MDN links: toString() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString; parseInt() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt – Grokify Aug 16 '15 at 17:39
  • Thanks Grokify, if I understand correctly though the `toString` and `parseInt` methods you mention are made to work with numbers and not strings. – Pensierinmusica Aug 16 '15 at 18:39
  • Take a look at the accepted answer here which implements `String.prototype.hexEncode` and `String.prototype.hexDecode` methods as wrappers to `toString` and `parseInt`. Then you can run something like `document.write("Hello World!".hexEncode().hexDecode());`. Link: http://stackoverflow.com/questions/21647928/javascript-unicode-string-to-hex – Grokify Aug 16 '15 at 18:41
  • Thanks, I looked at it already, as you mentioned earlier. It doesn't seem to work properly though (it returns, but the encoding is not correct). Actually I'd remove the link from the answer, if you agree. In the meantime, I added a working code example for NodeJS :) – Pensierinmusica Aug 16 '15 at 18:49
  • Got it. I removed the link and accepted your edit. Thanks! – Grokify Aug 16 '15 at 18:57
-3

Why not just store the 2 strings in different variables so no need to convert back?

To extract all alphanumerics you could use the regex function like so:

var str='dssldjf348902.-/dsfkjl';
var patt=/[^\w\d]*/g;
var str2 = str.replace(patt,'');

str2 becomes dssldjf348902dsfkjl

Trung Nguyen
  • 519
  • 3
  • 18