javascriptactionscriptencodingutf-8actionscript-2

actionscript 2 - escaping utf-8 characters


I am looking for a way to escape utf-8 characters using actionscript 2? as far as I know, in flash it uses utf-16 while on javascript it uses utf-8. I am looking for a similar escaping method.


Solution

  • Second comment on this post contains a custom StringUtil class intended to escape/unescape strings.


    For those who can't follow the link. (DISCLAIMER: Not my code, not tested by me)

    class StringUtil 
    { 
        public static function escape( str:String ):String 
        { 
            var a:Array  = str.split( "" ); 
            var i:Number = a.length; 
    
            while( i -- ) 
            { 
                var n:Number = a[ i ].charCodeAt( 0 ); 
    
                if( n >= 48 && n <= 56 ) 
                    continue; 
    
                if( n >= 65 && n <= 90 ) 
                    continue; 
    
                if( n >= 97 && n <= 122 ) 
                    continue; 
    
                if( n == 45 || n == 46 || n == 95 ) 
                    continue; 
    
                a[ i ] = "%" + n.toString( 16 ).toUpperCase(); 
            } 
    
            return a.join( "" ); 
        } 
    
        public static function unescape( str:String ):String 
        { 
            var a:Array  = str.split( "%" ); 
            var i:Number = a.length; 
    
            while( i -- > 1 ) 
            { 
                var n:Number = parseInt( a[ i ].substr( 0, 2 ), 16 ); 
    
                a[ i ] = String.fromCharCode( n ) + a[ i ].substr( 2 ); 
            } 
    
            return a.join( "" ); 
        } 
    }