spacer
Yehuda Shiran February 5, 2000
The ASCII Standard
Tips: February 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

The ASCII format is a standard for internal representation of characters. For example, the A character is internally represented in the computer by the ASCII value of 64, while the Z character is represented by 90. There are two basic functions to convert characters to their ASCII values and vice versa. The function charCodeAt(i) is a String method which converts the ith character to its ASCII value. The function fromCharCode(d) is also a String method which converts the ASCII value of d to a character.

Here is an example that uses these two functions. It implements a simple text encoding, designed to hide content of a message from casual users. It replaces each alphabetical character in the input text by its "complementary" character: A by Z, B by Y, etc:

function complement(s) {
  var sResult = "";
  var i = 0;
  var d = 0;
  
  for (i = 0; i < s.length; i++) {
    d = s.charCodeAt(i);
	if ((d >= 65) && (d <= 90)) {
	  d = 90 - (d -65);
    }
	sResult += String.fromCharCode(d);
  }

  return sResult;
}

And here is how you call this function to encode it twice:

var s = "A SECRET MESSAGE! ";
window.alert(s);
var sEncoded = complement(s);
window.alert(sEncoded);
s = complement(sEncoded);
window.alert(s);

The second encoding should give you the original message.

Learn more about the ASCII standard and its new replacement, the Unicode, in our Column 25, Unicode.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint