title: Fatal error: Not enough bits to represent the passed value
body: <p>Trying to use Mikrotik API library written in Swift:
  <a href="https://wiki.mikrotik.com/wiki/API_in_Swift" rel="nofollow noreferrer">https://wiki.mikrotik.com/wiki/API_in_Swift</a></p>
  
  <p>It works well, when I'm sending small commands</p>
  
  <p>However, If I will try to send large script string, I'm getting error:</p>
  
  <blockquote>
    <p>Fatal error: Not enough bits to represent the passed value</p>
  </blockquote>
  
  <p>The code that crashes:</p>
  
  <pre><code>private func writeLen(_ command : String) -&gt; Data {
      let data = command.data(using: String.Encoding.utf8)
      var len = data?.count ?? 0
      var dat = Data()
  
      if len &lt; 0x80 {
          dat.append([UInt8(len)], count: 1)
      }else if len &lt; 0x4000 {
          len = len | 0x8000;
          dat.append(Data(bytes: [UInt8(len &gt;&gt; 8)]))
          dat.append(Data(bytes: [UInt8(len)]))
      }else if len &lt; 0x20000 {
          len = len | 0xC00000;
          dat.append(Data(bytes: [UInt8(len &gt;&gt; 16)]))
          dat.append(Data(bytes: [UInt8(len &gt;&gt; 8)]))
          dat.append(Data(bytes: [UInt8(len)]))
      }
      else if len &lt; 0x10000000 {
          len = len | 0xE0000000;
          dat.append(Data(bytes: [UInt8(len &gt;&gt; 24)]))
          dat.append(Data(bytes: [UInt8(len &gt;&gt; 16)]))
          dat.append(Data(bytes: [UInt8(len &gt;&gt; 8)]))
          dat.append(Data(bytes: [UInt8(len)]))
      }else{
          dat.append(Data(bytes: [0xF0]))
          dat.append(Data(bytes: [UInt8(len &gt;&gt; 24)]))
          dat.append(Data(bytes: [UInt8(len &gt;&gt; 16)]))
          dat.append(Data(bytes: [UInt8(len &gt;&gt; 8)]))
          dat.append(Data(bytes: [UInt8(len)]))
      }
  
      return dat
  }
  </code></pre>
  
  <p>The fatal error appears in this part:</p>
  
  <pre><code>else if len &lt; 0x4000 {
      len = len | 0x8000;
      dat.append(Data(bytes: [UInt8(len &gt;&gt; 8)]))
      dat.append(Data(bytes: [UInt8(len)]))
  }
  </code></pre>
  
  <p>at line:</p>
  
  <pre><code>dat.append(Data(bytes: [UInt8(len)]))
  </code></pre>
  
  <p>Data size at this moment is 1072 bytes and <strong>len</strong> equals to 33840, UInt8 cannot be initiated with that <strong>len</strong> value.</p>
  
  <p>How can I edit the code to avoid the error?</p>
  
  <p>I'm using Swift 4.2</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here is an example of the same logic but written in JavaScript</p>
  
  <pre><code>module.exports.encodeString = function encodeString(s) {
  var data = null;
  var len = Buffer.byteLength(s);
  var offset = 0;
  if (len &lt; 0x80) {
      data = new Buffer(len + 1);
      data[offset++] = len;
  } else if (len &lt; 0x4000) {
      data = new Buffer(len + 2);
      len |= 0x8000;
      data[offset++] = (len &gt;&gt; 8) &amp; 0xff;
      data[offset++] = len &amp; 0xff;
  } else if (len &lt; 0x200000) {
      data = new Buffer(len + 3);
      len |= 0xC00000;
      data[offset++] = (len &gt;&gt; 16) &amp; 0xff;
      data[offset++] = (len &gt;&gt; 8) &amp; 0xff;
      data[offset++] = len &amp; 0xff;
  } else if (len &lt; 0x10000000) {
      data = new Buffer(len + 4);
      len |= 0xE0000000;
      data[offset++] = (len &gt;&gt; 24) &amp; 0xff;
      data[offset++] = (len &gt;&gt; 16) &amp; 0xff;
      data[offset++] = (len &gt;&gt; 8) &amp; 0xff;
      data[offset++] = len &amp; 0xff;
  } else {
      data = new Buffer(len + 5);
      data[offset++] = 0xF0;
      data[offset++] = (len &gt;&gt; 24) &amp; 0xff;
      data[offset++] = (len &gt;&gt; 16) &amp; 0xff;
      data[offset++] = (len &gt;&gt; 8) &amp; 0xff;
      data[offset++] = len &amp; 0xff;
  }
  data.utf8Write(s, offset);
  return data;
  };
  </code></pre>
  
  <p>Maybe someone sees the difference</p>