char code 0 vs. ByteArray

by valyard
Kind of unexpected result.
♥0 | Line 26 | Modified 2011-03-11 05:53:38 | MIT License
play

ActionScript3 source code

/**
 * Copyright valyard ( http://wonderfl.net/user/valyard )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/jaW2
 */

package {
    import flash.utils.ByteArray;    
    import com.actionscriptbible.Example;
    public class FlashTest extends Example {
        public function FlashTest() {
            var s:String = "a" + String.fromCharCode( 0 ) + "b" + String.fromCharCode( 0 );
            trace( s.length );
            trace( s.charCodeAt(0), s.charCodeAt(1), s.charCodeAt(2), s.charCodeAt(3) );
            var bytes:ByteArray = new ByteArray();
            bytes.writeUTF( s );
            bytes.writeUTFBytes( s );
            trace( "bytes:", bytes.length );
            bytes.position = 0;
            while ( bytes.bytesAvailable ) {
                trace( "-", bytes.readUnsignedByte() );
            }
            
            bytes.position = 0;
            s = bytes.readUTF();
            trace( s.length );
            trace( s.charCodeAt(0), s.charCodeAt(1), s.charCodeAt(2), s.charCodeAt(3) );
            s = bytes.readUTFBytes(4);
            trace( s.length );
            trace( s.charCodeAt(0), s.charCodeAt(1), s.charCodeAt(2), s.charCodeAt(3) );
        }
    }
}