views:

3183

answers:

3

I am having trouble escaping special characters in as3.

trace( escape("who are ü?") );

returns who%20are%20%uFFFD%3F

or

trace( encodeURIComponent("who are ü?") );

returns who%20are%20%EF%BF%BD%3F

while in javascript this

alert( encodeURIComponent("who are ü?") );

returns who%20are%20%C3%BC%3F

and

alert( escape("who are ü?") );

returns who%20are%20%FC%3F

Any suggestions how to get as3 to return escaped special characters as javascript is? Why is as3 apparently choking?

(here is a good reference: http://www.ultrashock.com/forums/actionscript/as3-escape-vs-as2-escape-122046.html )

+2  A: 

There is a small difference in URL escape and converting Unicode characters to their code form. What flex 3.0 escape is doing is probably URL escape whereas you want a decode for Unicode characters. Have you tried decodeURI? Hubris! Wrong answer :(

What I get with flex 3.0 is this:

who%20are%20%FC%3F

Isn't this what you want? Can you recheck? This is with a demo AIR app (and I am running Flex 3.0)!

dirkgently
Unfortunately, my trace statements are "who%20are%20%uFFFD%3F" (using flex3).
jedierikb
The code point %uFFFD flag tells you 'ü' did not get saved correctly. Did you make any changes to the locale? I believe that you are looking at console output.
dirkgently
I didn't change the locale... however, it looks like what i am looking for is utf-8 compliant encodeURIComponent for browser compatibility (which as3 isn't giving me either. i get "who%20are%20%EF%BF%BD%3F")
jedierikb
+3  A: 

Looks like AS is escaping the string as UTF-16, while the Javascript example is escaping as UTF-8. The specification for escape doesn't define how to deal with non-ascii characters, which makes it up to each implementation to decide what to do. Apparently Adobe/Macromedia chose to encode unicode points as UTF-16, while most browsers use UTF-8. You can use the function encodeURIComponent instead, which is defined as escaping as UTF-8 - This should be consistent across different implementations. If you require the AS behaviour, I don't think there is a native function in Javascript, but you can use the functions provided here.

troelskn
+1  A: 

Although my IDE (intellij) was displaying, saving, and loading special characters, they were being saved in 1252. Switching to UTF-8 has fixed this.

jedierikb