title: Not able to convert objective-c code to swift 4
body: <p>Can anyone please help me convert this objective-c function to swift4?
  I have tried Swiftify but it is giving wrong output. Also I have tried with string to data conversion but output is wrong.</p>
  
  <pre><code>- (NSData *)stringToData{
  if (!HCKBluetoothValidStr(self)) {
      return nil;
  }
  if (!(self.length % 2 == 0)) {
      return nil;
  }
  Byte bytes[255] = {0};
  NSInteger count = 0;
  for (int i =0; i &lt; self.length; i+=2) {
  
      NSString *strByte = [self substringWithRange:NSMakeRange(i,2)];
      NSLog(@"strbyte is: %@ \n\n",strByte);
      unsigned long red = strtoul([strByte UTF8String],0,16);
      Byte b =  (Byte) ((0xff &amp; red) );//( Byte) 0xff&amp;iByte;
      bytes[i/2+0] = b;
      count ++;
  }
  NSData * data = [NSData dataWithBytes:bytes length:count];
  return data;
  </code></pre>
  
  <p>}</p>
  
  <p>for same function input string is:</p>
  
  <pre><code>NSString *commandStr = @"1702030a0a";
  </code></pre>
  
  <p>and output from function in NSData format is:</p>
  
  <pre><code>&lt;1702030a 0a&gt;
  </code></pre>
  
  <h1>EDIT 1:</h1>
  
  <p>I have tried to convert it to swift, but strString.utf8CString giving error as "Cannot convert value of type 'ContiguousArray' (aka 'ContiguousArray') to expected argument type 'UnsafePointer!'"</p>
  
  <pre><code>func stringToData(stringVal: NSString) -&gt; NSData {
      var bytes: [UInt8] = [0]
      var count: Int = 0
      var i = 0
      for index in stride(from: 0, to: stringVal.length , by: 2) {
          var strByte: NSString = stringVal.substring(with: NSMakeRange(index, 2)) as NSString
          print("strbyte is: \(strByte) \n\n")
          let strString = String(strByte)
          print(strString)
          var red: UInt8 = strtoul(strString.utf8CString, 0, 16)
  
  
          let b = UInt8((0xff &amp; red))
          count = count+1
      }
      var data = NSData(bytes: bytes, length: count)
      return data
  }
  </code></pre>