Recipe 9.14 Converting Between Strings and Unicode or ASCII

9.14.1 Problem

You want to convert between characters and their corresponding Unicode code point (a.k.a. character codes) or ASCII codes.

9.14.2 Solution

Use the String.charCodeAt( ) and String.fromCharCode( ) methods.

9.14.3 Discussion

You can use fromCharCode( ) to display characters that you cannot enter into your Flash document directly. The method is a static method, which means that it is invoked from the top-level String object instead of from a string instance. For values less than 128, fromCharCode( ) essentially converts a numeric ASCII code to its equivalent character.

/* Outputs:    New paragraph: ¶    Cents: ¢    Name: Joey */ trace("New paragraph: " + String.fromCharCode(182)); trace("Cents: " + String.fromCharCode(162)); trace("Name: " + String.fromCharCode(74, 111, 101, 121));

You can use the charCodeAt( ) method to retrieve the code point of the character at a particular index of a string. For characters whose Unicode code point is less than 128, charCodeAt( ) essentially converts a character to its equivalent ASCII code.

myString = "abcd"; // Outputs the code point, 97, of the first character, a trace(myString.charCodeAt(0));

The fromCharCode( ) method is an alternative to using Unicode escape sequences to display special characters. However, you can also use fromCharCode( ) in concert with charCodeAt( ) to test for the existence of special characters:

myString = String.fromCharCode(191) + "Donde es el ba" +             String.fromCharCode(241)  + "o?"; // Test whether the first character of the string has the code point of 191. If so, // displays: The string "¿Donde es el baño?" has a ¿ at the beginning. if (myString.charCodeAt(0) == 191) {   message = "The string \"" + myString + "\" has a " + String.fromCharCode(191) +              " at the beginning.";   trace(message); }

The following methods are useful for creating cryptographic word games, but they are not secure and should not be used for sensitive data.

You can use the charCodeAt( ) and fromCharCode( ) methods in concert to encode and decode a string:

String.prototype.encode = function (  ) {   // The codeMap property is assigned to the String class when the encode(  ) method is   // first run. Therefore, if no codeMap is defined, it needs to be created.   if (String.codeMap == undefined) {     // The codeMap property is an associative array that maps each original code      // point to another code point.     String.codeMap = new Object(  );     // Create an array of all the code points from 0 to 255.     var origMap = new Array(  );     for (var i = 0; i <= 255; i++) {       origMap.push(i);     }     var rand;     // Create a temporary array that is a copy of the origMap array.     var charTemp = origMap.concat(  );     // Loop through all the character code points in origMap.     for (var i = 0; i < origMap.length; i++) {       // Create a random number that is between 0 and the last index of charTemp.       rand = Math.round(Math.random(  ) * (charTemp.length-1));       // Assign to codeMap values such that the keys are the original code points and       // the values are the code points to which they should be mapped.       String.codeMap[origMap[i]] = charTemp[rand];       // Remove the elements from charTemp that were just assigned to codeMap. This       // prevents duplicates.       charTemp.splice(rand, 1);     }   }   // Split the string into an array of characters.   var chars = this.split("");   // Replace each character in the array with the corresponding value from codeMap.   for (var i = 0; i < chars.length; i++) {       chars[i] = String.fromCharCode(String.codeMap[chars[i].charCodeAt(0)]);   }   // Return the encoded string.   return chars.join(""); }; String.prototype.decode = function (  ) {   // Split the encoded string into an array of characters.   var chars = this.split("");   // Create an associative array that reverses the keys and values of codeMap. This   // allows you to do a reverse lookup based on the encoded character rather than the   // original character.   var reverseMap = new Object(  );   for (var key in String.codeMap) {     reverseMap[String.codeMap[key]] = key;   }   // Loop through all the characters in the array and replace    // them with the corresponding value from reverseMap, thus    // recovering the original character values.   for (var i = 0; i < chars.length; i++) {     chars[i] = String.fromCharCode(reverseMap[chars[i].charCodeAt(0)]);   }   // Return the decoded string.   return chars.join(""); }; // Example usage: myString = "She sells sea shells by the sea shore"; // Create the encoded version of myString using the encode(  ) method. encoded = myString.encode(  ); // Output the value of the encoded string. This will be randomly generated each time // you run the movie. It might look something like this: // êEk¶°k§§°¶°k¬¶°Ek§§°¶v_¶¥Ek¶°k¬¶°E _k trace(encoded); // Output the value returned by the decode(  ) method.  // Displays: She sells sea shells by the sea shore trace(encoded.decode(  ));

9.14.4 See Also

Table A-1 lists the Unicode code points for the Latin 1 character set.



ActionScript Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2005
Pages: 425

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net