Buca Bay - Always nice

Dua tiko noqu toa loaloa, na yacana ko… laga mai…

ASCII to Hex in JavaScript

June14

This JavaScript will add a method to the String object, that converts the ASCII string to a hex string consisting of the equivalent character codes for each character in the String.

String.prototype.toHex = function() {
    return this.replace(/[\s\S]/g, function(s) {
        return parseInt(s.charCodeAt()).toString(16);
    });
};

Note that hex is base 16 and ASCII is based 256 so the hex string is a longer.

I recently had to save a password in hex (Hexadecimal) instead of plain ASCII since the software that came with my Netgear USB Wireless Adapter didn’t like the ASCII version for some reason.

So for example if your password is “joe”, then to convert it to the Hex equivalent would be:

'joe'.toHex()

Oddly, my computer (Vista) took the Hex password, while the printer took the ASCII version and my friends Windows 7 took the Hex.

Related posts:

  1. JavaScript Regex match all characters JavaScript regular expressions (browser) do not have a modifier allowing the dot (.) to match all characters including the newline...
  2. Base conversion in JavaScript I just realized recently that you can convert between number bases in JavaScript using the built in method Object.prototype.toString() and...
  3. Secure password hashing and storage in PHP Everyone knows you should never store passwords as plain text, right? Recently I’ve come across a lot of bad advice...
posted under javascript
2 Comments to

“ASCII to Hex in JavaScript”

  1. On June 15th, 2010 at 11:50 am marika lorosio MonsterID Icon marika lorosio Says:

    sa totolo vaca na up ni ka qo. lol. so is there a password on our network right now?

  2. On June 15th, 2010 at 12:03 pm gabe MonsterID Icon gabe Says:

    Yes, but not on router.

Email will not be published

Website example

Your Comment:

 
Tag Cloud