title: scodec decode/encode split length field
body: <p>I am using scodec: <a href="https://github.com/scodec/scodec" rel="nofollow">https://github.com/scodec/scodec</a> to decode/encode a binary protocol.</p>
  
  <p>I am struggling with a part of the spec where a "length" field is split into two parts by a "moreflag".  The moreflag indicates if the length field needs more space.</p>
  
  <pre><code>Example:
  Byte 1:  identifier bits 8-7, moreFlag bit 6, length bits 5-0    // first length field filled with 0's if moreFlag is false
  Byte 2:  moreFlag bit 8, length bits 7-0  
  Byte 3:  otherJunk bits 8-0
  </code></pre>
  
  <p>My problem is I want to encode/decode both of these length fields into a single case class field:</p>
  
  <pre><code>case class Header(totalLength: Int, otherJunk: Int)
  </code></pre>
  
  <p>I have tried a few different things, but nothing has worked out so far:</p>
  
  <pre><code>implicit val headerCodec: Codec[Header] = (
    ("identifier" | uint2) :~&gt;:
    ("moreFlag" | bool).compact &gt;&gt;:~ { meta =&gt; 
      if (meta.last) {
        // more flag enabled, combine lengths somehow
        ("first length part" | uint(5)) :: ("moreFlag2DontCare" | uint(1) :~&gt;: ("second length part - how to combine?" | uint(7)) :: ("otherJunk" | uint8) 
      }
      else {
        ("first length part always 0s" | constant(bin"00000")) :: ("moreFlag2DontCare" | uint(1) :~&gt;: ("fullLength" | uint(7)) :: ("otherJunk" | uint8)
      }
    }
  ).as[Header]
  </code></pre>
  
  <p>Am I on the right track here?  Thanks!</p>