title: Converting PHP function to C#
body: <p>I've almost finished converting a PHP script to a C# one to use in ASP.net.</p>
  
  <p>I've already converted this to match the the correct result so far:-</p>
  
  <pre><code>function sign_url($url, $key, $secret)
  {
      if (strpos($url,'?') !== false)
      {
          $url .= "&amp;";
      }
      else
      {
          $url .= "?";
      }
      $url .= "ApplicationKey=" . $key;
  
      $signature = hash_hmac("sha1", urldecode($url), $secret);
      $url .= "&amp;Signature=" . hex_to_base64($signature);
  
      return $url;
  }
  </code></pre>
  
  <p>Here is the part i'm struggling with:-</p>
  
  <pre><code>function hex_to_base64($hex){
    $return = '';
    foreach(str_split($hex, 2) as $pair){
      $return .= chr(hexdec($pair));
    }
    return base64_encode($return);
  }
  </code></pre>
  
  <p>From what i can gather it splits the input string ($hex) into sets of 2 characters.<br/>
  It then converts these hexadecimal strings to decimal numbers before getting the chr() value of this number.<br/>
  Storing all these conversions in a $return string.<br/>
  Finally it does the base 64 conversion.<br/></p>
  
  <p>I'm struggling to find the best way to do this in C#, particularly the splitting into 2 characters while doing the other conversions. </p>