Archive

Posts Tagged ‘decode’

Encoding & Decoding functions in Flex

August 8, 2009 2 comments

The other day one of my colleagues asked me whether do we have any functions inside flex that can encode a URL . Even though it quite obvious that something can be done manually to replace the strings, but in order to find some answers I looked into the Top level packages where I found encodeURI() and decodeURI() . What does these functions actually do?
encodeURI():
This function bascially encodes the string into a valid URI. The following shown below are not encoded by encodeURI() unless it is a small group of basic characters as given in the documentation.

Characters that are not encoded
0 1 2 3 4 5 6 7 8 9
a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
; / ? : @ & = + $ , #
- _ . ! ~ * ' ( )

There is another encoding function that is slightly different from encodeURI()encodeURIComponent() which also does the same encoding of strings to URI but.Before we talk about the major difference we should be aware of some terms :

URIComponent: is the text that appears in between any special characters..for eg: http and http://www.google.com are the  URI components in http://www.google.com.
Component Seperatorsare the special characters used in a URI (: ; / ?)

Now coming back to the major difference the encodeURIComponent() in encodes the same as encodeURI() and additionally also encodes special characters (; / ? : @ & + $ , # )

decodeURI():
This function does the exact reverse of encodeURI() , it decodes the encoded URI into string where all encoded characters are restored into unencoded representation.

Escape sequence not decoded Characters equivalents
%23 #
%24 $
%26 &
%2B +
%2C ,
%2F /
%3A :
%3B ;
%3D =
%3F ?
%40 @

Using the decodeURIComponent() the escape sequence in the above table can be decoded.
All the above functions are in addition to the already existing escape() and unescape() functions which were also used in flash.escape() encodes all nonalphanumeric charachters into format % and hexadecimal sequence and unescape() decodes the URL encoded format to string.
So , there are plenty of things to play with when you need some strings to be encoded, each function does look similar but it has some variations which needs to be closely observed.Before concluding here is a small code snippets which includes the usage of above functions:


var redirectUrl:String = "http://www.somedomain.com?loggedin=true& username=Sun";
    
var escaped:String = escape(redirectUrl);
var unescaped:String = unescape(escaped);
    
var encoded:String = encodeURI(redirectUrl);
var decoded:String = decodeURI(encoded);
    
var encodedURI:String = encodeURIComponent(redirectUrl);
var decodedURI:String = decodeURIComponent(encodedURI);
    
trace("escaped: "+escaped); //escaped: http%3A//www.somedomain.com%3Floggedin%3Dtrue%26username%3DSun
trace("unescaped: "+unescaped); //unescaped:
http://www.somedomain.com?loggedin=true&username=Sun
    
trace("encoded: "+encoded); //encoded:
http://www.somedomain.com?loggedin=true&%20username=Sun
trace("decoded: "+decoded); //decoded: http://www.somedomain.com?loggedin=true&username=Sun
    
trace("encodedURI: "+encodedURI); //encodedURI: http%3A%2F%2Fwww.somedomain.com%3Floggedin%3Dtrue%26username%3DSun
trace("decodedURI: "+decodedURI); //decodedURI:
http://www.somedomain.com?loggedin=true&username=Sun


Meanwhile when I was googling more on this, I found out a bug reported to Adobe where it was reported that encodeURI and decodeURI misbehaves with httpservice which is mentioned to be deferred and will be fixed in 4.0 hmm..I am curious to know about your experiences when using these functions…

Follow

Get every new post delivered to your Inbox.