title: Objective-C Packing Data using NSMutableData?
body: <p>I need to put a short and integer at the begging of a message that i am sending to a java server. The server is expecting to read a short (message id) then an integer (message length). I've read in the stackoverflow that NSMutableData is similar to java ByteBuffer.</p>
  
  <p>I am trying to pack the message into NSMutableData then send it.</p>
  
  <p>So this is what I have but is not working !.</p>
  
  <pre><code>NSMutableData *data = [NSMutableData dataWithLength:(sizeof(short) + sizeof(int))];
  
  short msg_id = 2;
  int length = 198;
  
  [data appendBytes:&amp;msg_id length:sizeof(short)];
  [data appendBytes:&amp;length length:sizeof(int)];
  
  send(sock, data, 6, 0);
  </code></pre>
  
  <p>The server is using Java ByteBuffer to read in the received data. So the bytes coming in is:</p>
  
  <pre><code>32,120,31,0,2,0
  </code></pre>
  
  <p>which is invalid. </p>
  
  <p>The correct value so the ByteBuffer can read them as .getShort() and .getInt() </p>
  
  <pre><code>0,2,0,0,0,-66
  </code></pre>