Recipe 12.12. Converting Between Strings and Unicode or ASCII


Problem

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

Solution

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

Discussion

You can use fromCharCode( ) to display characters you cannot directly enter into your Flash document. The method is a static method, which means that it is invoked from the top-level String object instead of from a string instance. It takes an integer or a series of integers and coverts the character codes to their string equivalents. When values less than 128 are used, fromCharCode( ) essentially converts a numeric ASCII code to its equivalent character:

/* Displays:    New paragraph: ¶    Cents: ¢    Name: Darron */ trace( "New paragraph: " + String.fromCharCode( 182 ) ); trace( "Cents: " + String.fromCharCode( 162 ) ); trace( "Name: " + String.fromCharCode( 68, 97, 114, 114, 111, 110 ) );

The charCodeAt( ) method can be used 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( ) converts a character to its equivalent ASCII code:

var example:String = "abcd"; // Outputs the code point, 97, of the first character, a. trace( example.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:

var example:String = String.fromCharCode( 191 ) + "D"             + String.fromCharCode( 243 ) + "nde est"            + String.fromCharCode( 225 ) + " el ba"             + String.fromCharCode( 241 )  + "o?"; // Test whether the first character of the string has the code point of // 191. Use the unicode escape sequence instead of fromCharCode( 191 ) to // produce the speical character. If so displays: The string "¿Dónde está  // el baño?" has a ¿ at the beginning. if ( example.charCodeAt( 0 ) == 191 ) {   trace( "The string \"" + example + "\" has a \u00BF at the beginning." ); }

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

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

public static function encode( original:String ):String {   // The codeMap property is assigned to the StringUtilities class when the encode(  )    // method is first run. Therefore, if no codeMap is yet defined, it needs    // to be created.   if ( codeMap == null ) {     // The codeMap property is an associative array that maps each original code     // point to another code point.     codeMap = new Object(  );     // Create an array of all the code points from 0 to 255.     var originalMap:Array = new Array(  );     for ( var i:int = 0; i < 256 ; i++ ) {       originalMap.push( i );     }     // Create a temporary array that is a copy of the originalMap array.     var tempChars:Array = originalMap.concat(  );     // Loop through all the character code points in originalMap.     for ( var i:int = 0; i < originalMap.length; i++ ) {       // Create a random number that is between 0 and the last index of tempChars.       var randomIndex:int = Math.floor( Math.random(  ) * ( tempChars.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.       codeMap[ originalMap[i] ] = tempChars[ randomIndex ];       // Remove the elements from tempChars that was just assigned to codeMap.        // This prevents duplicates.       tempChars.splice( randomIndex, 1 );     }   }   // Split the string into an array of characters.   var characters:Array = original.split("");   // Replace each character in the array with the corresponding value from codeMap.   for ( i = 0; i < characters.length; i++ ) {     characters[i] = String.fromCharCode( codeMap[ characters[i].charCodeAt( 0 ) ] );   }   // Return the encoded string.   return characters.join( "" ); } public static function decode( encoded:String ):String {   // Split the encoded string into an array of characters.   var characters:Array = encoded.split( "" );   // The reverseCodeMap property is assigned the first time the decode(  ) method is    // first run. Therefore, if no reverseCodeMap is yet defined, it needs to be    // created.   if ( reverseCodeMap == null ) {            // 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.     reverseCodeMap = new Object(  );     for ( var key in codeMap ) {       reverseCodesMap[ codeMap[key] ] = key;     }   }   // Loop through all the characters in the array, and replace them   // with the corresponding value from reverseCodeMap, thus recovering   // the original character values.   for ( var i:int = 0; i < characters.length; i++ ) {     characters[i] = String.fromCharCode( reverseCodeMap[ characters[i].charCodeAt( 0 ) ] );   }   // Return the decoded string.   return characters.join( "" ); }

The following is a simple usage example:

var example:String = "Peter Piper picked a peck of pickled peppers."; // Create the encoded version of example using the encode(  ) method. var encoded:String = StringUtilities.encode( example ); // Output the value of the encoded string. This will be randomly generated  // each time you run the movie. It might look something like this: // æT#Tm&#239;æ~*Tm&#239;*~NcT&#173;&#239;?&#239;*TNc&#239;?2&#239;*~Nc?T&#173;&#239;*T**Tm:V trace( encoded ); // Output the value returned by the decode(  ) method:  // Displays: Peter Piper picked a peck of pickled peppers. trace( StringUtilities.decode( encoded ) );




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

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