0 Replies Latest reply on Nov 9, 2009 4:26 PM by jeremy.banks

    UTF-16 Encoding

    jeremy.banks

      Hi

       

      I have a web service that a flex app connects to.  I'm trying to change the service to use UTF-16 encoding, but I'm struggling to get Flex to successfully read the data returned.

       

      Some simplified code:

       

            private function _loginReturned(event:Event):void

            {

               var request:URLRequest = new URLRequest(_restUrl + "user");

               var stream:URLStream = new URLStream();

               stream.addEventListener(Event.COMPLETE, _streamReturned);

               stream.load(request);

            }

       

            private function _streamReturned(event:Event):void

            {

               var stream:URLStream = (event.target as URLStream);

               trace("stream: " + stream.readMultiByte(stream.bytesAvailable, "unicode"));

            }

       

      This traces "stream: "

       

      If I change _streamReturned to:

            private function _streamReturned(event:Event):void

            {

               var stream:URLStream = (event.target as URLStream);

               while (0 < stream.bytesAvailable)

               {

                  trace("stream: " + stream.readMultiByte(1, "unicode"));

               }

            }

       

      Then I get a trace of:

      stream:

      stream: <

      stream:

      stream: ?

      stream:

      stream: x

      stream:

      stream: m

      stream:

      stream: l

      stream:

      stream: 

      stream:

      stream: v

      stream:

      stream: e

      stream:

      stream: r

      stream:

      stream: s

      stream:

      stream: i

      stream:

      stream: o

      ...
      Indicating that every character is being encoded within two bytes, which is about what I'd expect.
      Any ideas why stream.readMultiByte(stream.bytesAvailable, "unicode") doesn't return anything useful?
      The backend is set to encode using UTF-16BE, but I have tried it with UTF-16 and UTF-16LE as well with no luck.  I also tried using unicodeFFFE in the readMultiByte call but also with no luck.
      Thanks in advance for your help.
      Jeremy