title: how to convert array of bytes to base64 String in iphone?
body: <p>I have a piece of code in vb. I need to convert array of bytes to base 64 string. Following is the vb code.</p>
  <blockquote>
  <p>If arrLicence.Count &gt; 0 Then</p>
  <p>LicenceBytes = CType(Array.CreateInstance(GetType(Byte),6), Byte())</p>
  <pre><code>        LicenceBytes(0) = Convert.ToByte(arrLicence(0).ToString(), 16)
          LicenceBytes(1) = Convert.ToByte(arrLicence(1).ToString(), 16)
          LicenceBytes(2) = Convert.ToByte(arrLicence(2).ToString(), 16) 
          LicenceBytes(3) = Convert.ToByte(arrLicence(3).ToString(), 16) 
          LicenceBytes(4) = Convert.ToByte(arrLicence(4).ToString(), 16)
          LicenceBytes(5) = Convert.ToByte(arrLicence(5).ToString(), 16)
  
          LicenceString = Convert.ToBase64String(LicenceBytes) '6 byteArray - passed by the user - Base64Encoded
  </code></pre>
  </blockquote>
  <p>I need its equivalent in iphone. I tried with NSData and base64 conversion but result defers.</p>
  <p>I have used this link for conversion.
  <a href="http://www.cocoadev.com/index.pl?BaseSixtyFour" rel="nofollow noreferrer">http://www.cocoadev.com/index.pl?BaseSixtyFour</a></p>
  <p>I tried by creating individual bytes using <code>memcpy</code> and then creating an array but with no success.</p>
  <p>What I have tried is as follows:</p>
  <pre><code>NSData *d1 =[@&quot;64&quot; dataUsingEncoding:NSUTF16StringEncoding];
  NSData *d2 = [@&quot;37&quot; dataUsingEncoding:NSUTF16StringEncoding];
  NSData *d3 = [@&quot;81&quot; dataUsingEncoding:NSUTF16StringEncoding];
  NSData *d4 = [@&quot;d4&quot; dataUsingEncoding:NSUTF16StringEncoding];
  
  unsigned char *buffer = (unsigned char*)malloc(8);
  buffer[0] =  [d1 bytes]  ;
  buffer[1] =  [d2 bytes] ;
  buffer[2] =  [d3 bytes] ;
  buffer[3] =  [d4 bytes] ;
  
  NSData *data = [NSData dataWithBytes:buffer length:4];
  
  NSString *str = [self encodeBase64WithData:data];
  free(buffer);
  </code></pre>
  <p>This results in <strong><code>IJCgkA==</code></strong> while  code in .NET returns <strong><code>ZDeB1A==</code></strong></p>
  <p><em><strong>Please note that the conversion is for first four bytes of arrLicence and the input is 64, 37, 81, d4</strong></em></p>