title: Sending floating point numbers to QuartzComposer via UDP with C++
body: <p>In the <a href="https://stackoverflow.com/questions/5909254/additional-characters-in-nsstring-for-network-transfer">another question</a> a user provided a method to send data to QuartzComposer via UDP. It appears that QuartzComposer is very particular about the padding of byte data sent to it. In particular it requires that each character sent be preceded with "\0\0\0".</p>
  
  <p>I was able to mock up a quick Python program to solve this problem:</p>
  
  <pre><code>from socket import *
  
  PORT = 50000
  N = 3.14159265358
  
  # connect to Quartz Composer on localhost port 50000
  s = socket(AF_INET, SOCK_DGRAM)
  s.bind(('', 0))
  s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
  
  for c in str(N):
      out = out + '\0\0\0' + c
  s.sendto(out, ('225.0.0.0', PORT))
  </code></pre>
  
  <p>The critical section which I need to implement in C++ is the bit which pads a floating point number. Here is a quick Python mock up of the critical section:</p>
  
  <pre><code>def padfloat(n):
      out = ''
      for c in str(n):
          out = out + '\0\0\0' + c
      return out
  
  if __name__ == '__main__':
      print(padfloat(3.14159265358))
  </code></pre>
  
  <p>Admittedly, my C++ chops are not so tight but I was able to use boost to get UDP up and running and to send a single hard coded message "0.7":</p>
  
  <pre><code>//
  // Working C++ to Quartz Network Test
  // 
  
  #include &lt;cstdlib&gt;
  #include &lt;cstring&gt;
  #include &lt;iostream&gt;
  #include &lt;boost/asio.hpp&gt;
  
  using boost::asio::ip::udp;
  
  enum { max_length = 1024 };
  
  int main(int argc, char* argv[])
  {
    try
    {
  boost::asio::io_service io_service;
  
  udp::socket s(io_service, udp::endpoint(udp::v4(), 0));
  
  udp::resolver resolver(io_service);
  udp::resolver::query query(udp::v4(), "225.0.0.0", "50000");
  udp::resolver::iterator iterator = resolver.resolve(query);
  
  using namespace std; // For strlen.
  
  // TODO: alter code to take in any number
  
  for (;;) {
    std::cout &lt;&lt; "Press Any Key to send 0.7: ";
    char request[max_length];
    std::cin.getline(request, max_length);
    // size_t request_length = strlen(request);
    char test [] = {0,0,0,'0',0,0,0,'.',0,0,0,'7','\0'};
    s.send_to(boost::asio::buffer(test, 12), *iterator);
    std::cout &lt;&lt; "Sent\n";
  }
    }
    catch (std::exception&amp; e)
    {
  std::cerr &lt;&lt; "Exception: " &lt;&lt; e.what() &lt;&lt; "\n";
    }
    return 0;
  }
  </code></pre>
  
  <p>The rather trivial problem I need help with is how to take in a floating point number and to properly format it as a char array to send out with all the "\0\0\0" padding properly prepended. I'm about to do something ugly, but if someone can point me to an elegant fix, I'd be quite happy to learn a bit.</p>