Creating a Bitcoin address using the 9 steps. Stuck on stage 2 I need to write a script using C++ and the OPENSSL library that takes this: 0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352
performs a SHA256 Hash using the sha256 function from the OpenSSL library and then returns 0b7c28c9b7290c98d7438e70b3d3f7c848fbd7d1dc194ff83f4f7cc9b1378e98 as the digest. 

The issue I am having is that I get a different message digest after performing the sha256 hash function: a9ce83de3a0ff3516b7c50cdd787e9f69f152f227d93c9512774231e7132e925.

I've been stuck on this for about 2 weeks and from my research I have been told that my function is not doing the right conversion of my message before performing the Sha256 hash function. 

Code

string sha256(const string str)
{
  unsigned char hash[SHA256_DIGEST_LENGTH];
  SHA256_CTX sha256;
  SHA256_Init(&amp;sha256);
  SHA256_Update(&amp;sha256, str.c_str(), str.size());
  SHA256_Final(hash, &amp;sha256);
  stringstream ss;

  for(int i = 0; i &lt; SHA256_DIGEST_LENGTH; i++)
  {
    ss &lt;&lt; hex &lt;&lt; setw(2) &lt;&lt; setfill('0') &lt;&lt; (int)hash[i];
  }
return ss.str();
}

int main()
{

  cout &lt;&lt; endl;
  cout &lt;&lt; sha256("0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352") &lt;&lt; endl;
  return 0;
}
The problem is that you are hashing the string of hex characters instead of the actual bytes that the hex string represents. You should be using an array instead with the bytes specified. The following should work:

string sha256(char[] str, size_t len)
{
  unsigned char hash[SHA256_DIGEST_LENGTH];
  SHA256_CTX sha256;
  SHA256_Init(&amp;sha256);
  SHA256_Update(&amp;sha256, str, len);
  SHA256_Final(hash, &amp;sha256);
  stringstream ss;

  for(int i = 0; i &lt; SHA256_DIGEST_LENGTH; i++)
  {
    ss &lt;&lt; hex &lt;&lt; setw(2) &lt;&lt; setfill('0') &lt;&lt; (int)hash[i];
  }
return ss.str();
}

int main()
{
  char[] arr = {0x02, 0x50, 0x86, 0x3a, 0xd6, 0x4a, 0x87, 0xae, 0x8a, 0x2f, 0xe8, 0x3c, 0x1a, 0xf1, 0xa8, 0x40, 0x3c, 0xb5, 0x3f, 0x53, 0xe4, 0x86, 0xd8, 0x51, 0x1d, 0xad, 0x8a, 0x04, 0x88, 0x7e, 0x5b, 0x23, 0x52};
  cout &lt;&lt; endl;
  cout &lt;&lt; sha256(arr, 32) &lt;&lt; endl;
  return 0;
}