title: Converting between Word8 and Word16 in Haskell
body: <p>I am trying to to little endian conversion in haskell, so that I can turn a Word16 into two Word8 (for example 258 = 1*256 + 2, so result should be [2, 1]). I then pack the result into a ByteString.</p>
  
  <p>I created the following code for this purpose:</p>
  
  <pre><code>import Data.Word
  import Data.Bits
  
  getByte b num = shift (relevantBits b num) (shiftNum b)
      where bitMask b = sum $ map (2^) [8*b-8 .. 8*b-1]
            relevantBits b num = num .&amp;. bitMask b
            shiftNum b = 8-8*b
  
  encodeWord16 x = [getByte 1 x, getByte 2 x]
  
  input :: Word16
  input = 355
  
  output :: [Word8]
  output = encodeWord16 input
  </code></pre>
  
  <p>The function <code>getByte</code> gets Byte number <code>b</code> from a number <code>num</code>. The function <code>encodeWord16</code> uses this helper function to do the little endian conversion.</p>
  
  <p>This however does not compile, I get the error:</p>
  
  <pre><code>Couldn't match expected type `Word8' with actual type `Word16'
  In the first argument of `encodeWord16', namely `input'
  In the expression: encodeWord16 input
  In an equation for `output': output = encodeWord16 input
  </code></pre>
  
  <p>I (very unsystematically) tried to achieve the desired result by randomly distributing <code>fromIntegral</code> expressions, but obviously my understanding of the haskell type system is not good enough to solve this problem. Is there a systematic way to approach this problem?
  Basically I want the function <code>encodeWord16</code> to have the type signature <code>Word16 -&gt; [Word8]</code>.</p>