1 Reply Latest reply on Oct 5, 2007 4:56 PM by buabco

    ByteArray To String Error

    buabco Level 1
      HI!,

      I've been having problems when using bytearray to comunicate with the server. In orther to do this I have to turn the byteArray into a String I've tried for this all 3 string convetrsion methods: readUTFBytes, readBytes and toString().

      Al three seemed to work fine until I started compressing the data, and realized that for some reason when getting a byte 10 in the array the to string conversion stop, even though the pointer move all the way to the end of the array anyways.

      Witch should be the right way to do this?

      Thanks
        • 1. Re: ByteArray To String Error
          buabco Level 1
          OK Complementing my previous post.


          I've found that the actual character that seems to cause problem is when you have the charcode(0) it seems that this is and end of string character when you pass the byteArray to string. Also curiously if you do this:

          var ba:ByteArray = new ByteArray();

          --- FILL BYTE ARRAY HERE WITH SOME DATA --

          ba.position = 0;
          trace(ba.readUTFBytes(ba.length));
          ba.position = 0;
          trace(ba.readMultiBytes(ba.length, "utf-8"));
          trace(ba.toString());

          They all return different values, specially if there is a 0 around, witch tent to cut the string short, the only one that returned the right string was the toString function, but then there still is a problem, since you can't concatenate this String or else the 0 will return a wrong string, if you do:

          trace(ba.toString() + "some extra text");

          You get a string only to the byte with 0.

          Since the manual indicated that the toString function does some converting of the byteArray, I decided that it will be more secure to do something like this:

          var tmp2:ByteArray = new ByteArray();
          for (var n:int = 0; n < s.length ; n++){
          tmp2.writeByte(s.charCodeAt(n));
          }

          witch actually returned a different byteArray than temp2.writeUTFBytes(s) when there is a character code 0 on the String.

          I still think that this might be a bug on the String object or there might be a problem with the ByteArray, or I still haven't figure out if there is a right way to do this.