5 Replies Latest reply on Dec 20, 2007 6:39 AM by 796440

    bit[] to byte[] and back

    807603
      I am trying to encode data into a file for loading in another program (map editor | Game)
      I have an array of booleans, which represent if tiles are walkable or not.

      I am trying to convert the booleans into bytes.
      I have code to convert bytes[] to chars, and back from an open-source example online, but its very confusing.

      I'm just looking for guidance on how to do bit operations.

      This is what I have:
           static public char[] encode(boolean[] data) {
                byte[] out = new byte[data.length / 8];
                
                for (int i = 0; i < data.length; i++) {
                     byte b = ((data) ? 1 : 0) << i%8; // !error: cannot cast from int to byte
                     
                }
                
      //return (encode to base 64 (chars));
           }
      
      Edited by: Anthony42 on Dec 19, 2007 2:58 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
        • 1. Re: bit[] to byte[] and back
          807603
          Your error is because you need to explicitly cast:
          byte b = (byte)((data[i] ? 1 : 0) << i%8);
          • 2. Re: bit[] to byte[] and back
            807603
            Ahh sweet.

            I tried that, but didn't put parenthesis around the entire statement.
            Am I going on the right path with this?
            • 3. Re: bit[] to byte[] and back
              807603
              A few problems:
              byte[] out = new byte[data.length / 8];
              If data.length is not a multiple of 8, the array will be too short.

              Regarding your error, you need to cast the int to byte explicitly.

              You're not storing it in the array, but when you do, you must OR it into the array, not just store it.
              • 4. Re: bit[] to byte[] and back
                807603
                I got the or part. Thanks for letting me know about the array size, I had missed that.

                This is what I have and it seems to be working:

                     
                static public char[] encode(boolean[] data) {
                          byte[] out = new byte[1 + (data.length / 8)];
                          for (int i = 0; i < data.length; i++) {
                               byte shift = (byte)(i%8);
                               byte value = (byte)(data[i] ? 1 : 0);
                               byte mask = (byte)(value << shift);
                               out[i/8] |= mask;
                          }
                          
                          return encode(out);
                     }
                I tried doing it all in one statement and it wasnt working correctly, so I broke them up to debug, and it worked... when I tried putting them back into a single statement it broke again. So I think Ill just leave it how it is. =D

                I need to write the reverse method now, and get the maps to load. Thats the real test of weather or not it worked correctly. =D
                • 5. Re: bit[] to byte[] and back
                  796440
                  byte[] out = new byte[1 + (data.length / 8)];
                  length = 1 --> 1 + 0 --> 1, OK
                  length = 7 --> 1 + 0 --> 1, OK
                  length = 8 --> 1 + 1 --> 2, Extra byte

                  What you want is
                  byte[] out = new byte[(data.length + 7) / 8];
                  length = 1 --> 8 / 8 --> 1, OK
                  length = 7 --> 14 / 8 --> 1, OK
                  length = 8 --> 15 / 8 --> 1, OK
                  length = 9 --> 16 / 8 --> 2, OK