title: A better way to work with 6 bit chunks
body: <h3>A more elegant way to do this?</h3>
  
  <p>I'm wondering if there is some way to use the functional programing aspects of swift to chunk an array into <strong>6-bit chunks</strong></p>
  
  <p>What I have works but it just doesn't <em>feel</em> like its the best way to do things</p>
  
  <h2>PseudoCode</h2>
  
  <p>I figured there is 4 types masking possible</p>
  
  <pre><code>[1111 1122]  [2222 3333]  [3344 4444]
  </code></pre>
  
  <ul>
  <li>first 6 bits</li>
  <li>last 2 bits and first 4 bits of next byte</li>
  <li>last 4 bits and first 2 bits of next byte</li>
  <li>last 6 bits</li>
  </ul>
  
  <p>I've put together some code that basically iterates through the bytes, calculates which "type" of masking we are doing - and masks appropriately.  It just feels somewhat rough - perhaps there is a way to use some of the functional commands like <code>map</code> to break the byte structure into <strong>6-bit chunks</strong>.  </p>
  
  <h2>Step 1 - Convert into a byte array</h2>
  
  <p>The first thing i do is take my <code>NSData</code> and break it up into an array of <code>UInt8</code></p>
  
  <pre><code>let ptr = UnsafeMutablePointer&lt;UInt8&gt;(rawNSData.bytes)
  let bytes = UnsafeMutableBufferPointer&lt;UInt8&gt;(start: ptr, count: rawNSData.length)
  </code></pre>
  
  <h2>Step 2 -</h2>
  
  <p>Next I wrote this parser which basically tracks what "byte" we are on and which "type" of masking we need to perform (see above).  </p>
  
  <pre><code>func parseDLAC(data : NSData, startingByte startingDataLocation : Int) -&gt; String {
  
      var ret = "";
  
      ///Tracks the current "decoding" - so that we can figure out which type of masking to do
      var currentDLACNumber = 0
  
      while (true) {
  
          // 4 types of masking possible
          let maskIteration = currentDLACNumber % 4
  
          ///Where to start the current decoding
          let startingByte = (currentDLACNumber * 6) / 8 + startingDataLocation
          let endingByte   = ((currentDLACNumber + 1 ) * 6) / 8 + startingDataLocation
  
          if (endingByte &gt; data.length) {
              return "ERROR";
          }
  
          var rawValue : UInt8
  
          switch(maskIteration) {
  
          case 0:  // grab 1st 6 bits and shift right
              rawValue = (bytes[startingByte] &amp; 0xFC) &gt;&gt; 2
          case 1:  // 2 &amp; 4
              rawValue = ((bytes[startingByte] &amp; 0x03) &lt;&lt; 4) | ((bytes[startingByte+1] &amp; 0xF0) &gt;&gt; 4)
          case 2:
              rawValue = ((bytes[startingByte] &amp; 0x0F ) &lt;&lt; 2) | ((bytes[startingByte+1] &amp; 0xC0) &gt;&gt; 6)
          case 3:
              rawValue = (bytes[startingByte] &amp; 0x3F)
          default:
              rawValue = 0
          }
  
          if (rawValue == 0) {
              return ret
          }
  
          ret += decodeDLAC(rawValue);
  
  
          // Next!
          currentDLACNumber++
      }
      return ret;
  }
  </code></pre>
  
  <h3>Solution</h3>
  
  <p>I would love to know if there is a more elegant way to re-write this code.  </p>
  
  <p>Given NSData and a starting address (byte offset) is there a way to create an array of 6-bit chunks (UInt8's).  I'm confused about <code>map</code> because I know it "tries" to work with distinct array elements.  I also didn't know if there was an <code>UnsafeMutalbeBitPointer</code> type that perhaps could help.</p>
  
  <p>Thanks for any help - and yes - <strong>not possible</strong> is a valid answer.</p>