title: Convert hex data string to NSData in Objective C (cocoa)
body: <p>fairly new iPhone developer here. Building an app to send RS232 commands to a device expecting them over a TCP/IP socket connection. I've got the comms part down, and can send ASCII commands fine. It's the hex code commands I'm having trouble with.</p>
  
  <p>So lets say I have the following hex data to send (in this format): </p>
  
  <p>\x1C\x02d\x00\x00\x00\xFF\x7F</p>
  
  <p>How do I convert this into an NSData object, which my send method expects?</p>
  
  <p>Obviously this does not work for this hex data (but does for standard ascii commands):</p>
  
  <pre><code>NSString *commandascii;
  NSData *commandToSend;
  commandascii = @"\x1C\x02d\x00\x00\x00\xFF\x7F";
  commandToSend = [commandascii dataUsingEncoding:NSStringEncoding];
  </code></pre>
  
  <p>For a start, some of the \x hex codes are escape characters, and I get an "input conversion stopped..." warning when compiling in XCode. And NSStringEncoding obviously isn't right for this hex string either. </p>
  
  <p>So the first problem is how to store this hex string I guess, then how to convert to NSData.</p>
  
  <p>Any ideas?</p>