title: Return unsigned char as a hex value
body: <p>I'm aiming to be able to return unsigned char as a hex values, this is so I can pass the value from my client to server side. </p>
  
  <p>How my data is generated before trying to be converted into hex:</p>
  
  <pre><code>unsigned char *TestClass::GetKey()
  {
      // Generate a key of length 32 bytes
      memset(this-&gt;szKey, 0, 32);
      RAND_bytes(this-&gt;szKey, 32);
  
      return this-&gt;szKey;
  }
  </code></pre>
  
  <p>This what i've currently got so far:</p>
  
  <pre><code>TestClass myTestClass;
  
  void CJSCallDoc::OnDocumentComplete(LPCTSTR strURL,LPDISPATCH pDisp)
  {
      unsigned char szKey = hex_print(myTestClass.GetKey());
  }
  
  unsigned char CJSCallDoc::hex_print(unsigned char* pv)
  {
      unsigned char *p = pv;
      if (NULL == pv)
      {
          return NULL;
      }
      else
      {
          char storedString[256];
  
          size_t len = strlen((const char*)pv);
          size_t i = 0;
  
          for (; i &lt; len; ++i)
          {
              strcpy_s(storedString, 256, "Test");
              strcat_s(storedString, reinterpret_cast&lt;char*&gt;(*p++));
          }
  
          return *storedString;
      }
  }
  </code></pre>
  
  <p>The problem I'm having is with this line:</p>
  
  <pre><code>strcat_s(storedString, reinterpret_cast&lt;char*&gt;(*p++));
  </code></pre>
  
  <p>This line causes my application to crash and this is the following error I get:</p>
  
  <blockquote>
    <p>Unhandled exception at 0x01664467 in TestApp.exe: 0xC0000005: Access
    violation reading location 0x000000FE.</p>
  </blockquote>
  
  <p>and the error takes me to <code>tcscat_s.inl</code>:</p>
  
  <pre><code>---&gt; while ((*p++ = *_SRC++) != 0 &amp;&amp; --available &gt; 0)
  {
  }
  </code></pre>
  
  <p>However when I try and do the following it works fine:</p>
  
  <pre><code>unsigned char CJSCallDoc::hex_print(unsigned char* pv)
  {
      unsigned char *p = pv;
      if (NULL == pv)
      {
          return NULL;
      }
      else
      {
          char storedString[256];
  
          size_t len = strlen((const char*)pv);
          size_t i = 0;
  
          for (; i &lt; len; ++i)
          {
              strcpy_s(storedString, 256, "Test");
              strcat_s(storedString, "WORKS");
          }
  
          return *storedString;
      }
  }
  </code></pre>
  
  <p>Could someone explain to me, what I'm doing wrong and give me some advice in the right direction?</p>