title: How to print a byte as a two-digit hexadecimal character?
body: <p>I need to generate random GUIDs in C, in Windows. I have:</p>
  
  <pre><code>HCRYPTPROV hCryptProv = 0;
  BYTE pbBuffer[16];
  int i;
  if (!CryptAcquireContextW(&amp;hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
      exit(1);
  for (i = 0; i &lt; N; i++) {
      if (!CryptGenRandom(hCryptProv, 16, pbBuffer))
          exit(1);
      printf("%X%X%X%X-%X%X-%X%X-%X%X-%X%X%X%X%X%X\n", pbBuffer[0], pbBuffer[1], pbBuffer[2], pbBuffer[3],
                                                       pbBuffer[4], pbBuffer[5], pbBuffer[6], pbBuffer[7],
                                                       pbBuffer[8], pbBuffer[9], pbBuffer[10], pbBuffer[11],
                                                       pbBuffer[12], pbBuffer[13], pbBuffer[14], pbBuffer[15]);
  }
  </code></pre>
  
  <p>However, this prints any byte less than 0F as a single character (e.g., <code>0000-00-00-00-000000</code> if each <code>pbBuffer[j]</code> were <code>0</code>). I need every single byte printed to be two characters (e.g., <code>00000000-0000-0000-0000-000000000000</code> if each <code>pbBuffer[j]</code> were <code>0</code>). How can I do this?</p>