title: How to get byte[] into capnp::Data
body: <p>On the official website, there is a <a href="https://capnproto.org/cxx.html" rel="nofollow noreferrer">nice and relatively comprehensive example</a> of how one could use CapnProto for C++ serialisation. What is missing, is how to handle the second Blob type <code>capnp::Data</code>, as only <code>capnp::Text</code> is covered.</p>
  <p>Just for completeness, here is what the Schema Language says about the blob type:</p>
  <blockquote>
  <p>Blobs: Text, Data</p>
  <p>...</p>
  <ul>
  <li><p>Text is always UTF-8 encoded and NUL-terminated.</p>
  </li>
  <li><p>Data is a completely arbitrary sequence of bytes.</p>
  </li>
  </ul>
  </blockquote>
  <p>So, if I have the following schema</p>
  <pre><code>struct Tiding {
      id @0 :Text;
      payload @1 :Data;
  }
  </code></pre>
  <p>I can start building my message like this</p>
  <pre><code>::capnp::MallocMessageBuilder message;
  Tiding::Builder tiding = message.initRoot&lt;Tiding&gt;();
  
  tiding.setId(&quot;1&quot;);
  </code></pre>
  <p>At this point I got stuck. I can't do this:</p>
  <pre><code>typedef unsigned char byte;
  
  byte data[100];
  ... //populate the array
  tiding.setPayload(data)
  //error: no viable conversion from 'byte [100]' to '::capnp::Data::Reader'
  </code></pre>
  <p>So I mucked around a bit and saw that capnp::Data is wrapping <code>kj::ArrayPtr&lt;const byte&gt;</code>, but I was unable to somehow get a hold of an <code>ArrayPtr</code>, much less use it to set the Payload field for my message.</p>
  <p>I saw that there is a way to set the default value for the type <code>Data</code> (i.e. <code>payload @5 :Data = 0x&quot;a1 40 33&quot;;</code>), but the schema language doesn't really translate to C++ in this case, so that also didn't help me.</p>
  <p>I'd be grateful if somebody could point out what I am missing here. Also, how would I do this if I had <code>List(Data)</code> instead of just <code>Data</code> as the Payload in my schema?</p>