title: Convert byte array to string in PHP
body: <p>Being new to PHP I need to convert an array of bytes to a string that may be used in php function; <code>hash_hmac("sha256", $payload, $key, true)</code></p>
  
  <p>The <code>$key</code> will originate from a Guid, say <code>{6fccde28-de32-4c0d-a642-7f0c601d1776}</code>, that's converted to an array of bytes. As <code>hash_hmac</code> takes a string for both data and key I need to convert this byte array without loss due to non-printable chars.</p>
  
  <p>I'm using following PHP code:</p>
  
  <pre><code>function guidToBytes($guid) {
      $guid_byte_order = [3,2,1,0,5,4,6,7,8,9,10,11,12,13,14,15];
      $guid = preg_replace("/[^a-zA-Z0-9]+/", "", $guid);
      $result = [];
      for($i=0;$i&lt;16;$i++)
          $result[] = hexdec(substr($guid, 2 * $guid_byte_order[$i], 2));
      return $result;
  }
  
  $payload = utf8_encode("Some data string");
  $keyBytes = guidToBytes("6fccde28-de32-4c0d-a642-7f0c601d1776");
  $key = implode(array_map("chr", $keyBytes));
  
  $hash = hash_hmac("sha256", $payload, $key, true);
  $result = base64_encode($hash);
  </code></pre>
  
  <p>Equivalent code in c#:</p>
  
  <pre><code>var g = Guid.Parse("6fccde28-de32-4c0d-a642-7f0c601d1776");
  var key = g.ToByteArray();
  var data = "Some data string";
  var payload = Encoding.UTF8.GetBytes(data);
  
  using(var ha = new HMACSHA256(key)) {
     var hash = ha.ComputeHash(payload);
     var result = Convert.ToBase64String(hash);
  }
  </code></pre>
  
  <p>Substituting value in <code>$key</code> will produce same <code>base64</code> output in both languages, leaving <code>$key</code> to be the thing that's wrong/differs.</p>
  
  <p>So; how do I convert <code>$keyBytes</code> to <code>$key</code> without loss of data?</p>