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

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

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:


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger