1 Reply Latest reply on Nov 8, 2010 10:27 AM by stephanoc

    Reading an Even Parity Byte Stream

    stephanoc

      I'm using a socket connection and, strangely enough, the data is being sent with an even parity byte.  This means there are 7 data bits and 1 parity bit.

       

      If you haven't guessed by now, this means that both socket.readByte() and readUnsignedByte() both fail me, as that left most bit is actually just the parity setting.

       

      So, I know that in c i could just trim the final bit with something like:

       

      char letter = dataByte & 0x7F;

       

      How on earth do I get my 7 bit ASCII decimal out of this byte in actionscript?

       

      I thought maybe I could read it into a ByteArray and trim the unneeded bit, but honestly I'm grasping at straws here.

        • 1. Re: Reading an Even Parity Byte Stream
          stephanoc Level 1

          The issue I was having was that ActionScript has no way to read parity bits. So, when it saw 10101100 (44 with even parity bit flipped), it read that as -84. Now that may seem strange, as it did to me, so you'll want to read all about it on Wikipedia.

           

          Now, to fix this strange issue, we simply need to be sure that if the parity bit is flipped to 1, we flip it back to zero. Doing this is simple, because ActionScript reads the parity bit as a negative byte.

           

           

          var text:String = "";
          var tmp:int = socket.readByte();
          var charCode:int code;
          if(tmp < 0)  //check to see if the parity bit was flipped to 1
          {
               code
          =tmp & 0x7F;  //reset the parity bit to 0
          }
          else
          {
               code
          =tmp;  //nothing to do if parity bit is 0
          }
          text
          +=String.fromCharCode(code);

           

          Now, should ActionScript support the reading of parity bits?  I doubt that is in the cards.  Still, good to know that if you do find them they are easy to deal with.