title: Converting unsigned char * to hexstring
body: <p>Below code takes a hex string(every byte is represented as its corresponidng hex value)
  converts it to unsigned char * buffer and then converts back to hex string.
  This code is testing the conversion from unsigned char* buffer to hex string
  which I need to send over the network to a receiver process.
  I chose hex string as unsigned char can be in range of 0 to 255 and there is no printable character after 127.
  The below code just tells the portion that bugs me. Its in the comment.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include &lt;iomanip&gt;
  
  using namespace std;
  // converts a hexstring to corresponding integer. i.e "c0" - &gt; 192
  int convertHexStringToInt(const string &amp; hexString)
  {
    stringstream geek;
    int x=0;
  
    geek &lt;&lt; std::hex &lt;&lt; hexString;
    geek &gt;&gt; x;
  
    return x;
  }
  
  // converts a complete hexstring to unsigned char * buffer
  void convertHexStringToUnsignedCharBuffer(string hexString, unsigned char* 
  hexBuffer)
  {
    int i=0;
    while(hexString.length())
    {
      string hexStringPart = hexString.substr(0,2);
      hexString = hexString.substr(2);
      int hexStringOneByte = convertHexStringToInt (hexStringPart);
      hexBuffer[i] = static_cast&lt;unsigned char&gt;((hexStringOneByte &amp; 0xFF)) ;
      i++;
    }
  }
  
  int main()
  {
    //below hex string is a hex representation of a unsigned char * buffer.
    //this is generated by an excryption algorithm in unsigned char* format
    //I am converting it to hex string to make it printable for verification pupose.
    //and takes the hexstring as inpuit here to test the conversion logic.
    string inputHexString = "552027e33844dd7b71676b963c0b8e20";
    string outputHexString;
    stringstream geek;
  
    unsigned char * hexBuffer = new unsigned char[inputHexString.length()/2];
    convertHexStringToUnsignedCharBuffer(inputHexString, hexBuffer);
  
    for (int i=0;i&lt;inputHexString.length()/2;i++)
    {
      geek &lt;&lt;std::hex &lt;&lt; std::setw(2) &lt;&lt; std::setfill('0')&lt;&lt;(0xFF&amp;hexBuffer[i]); // this works
      //geek &lt;&lt;std::hex &lt;&lt; std::setw(2) &lt;&lt; std::setfill('0')&lt;&lt;(hexBuffer[i]); -- &gt; this does not work
      // I am not able to figure out why I need to do the bit wise and operation with unsigned char "0xFF&amp;hexBuffer[i]"
      // without this the conversion does not work for individual bytes having ascii values more than 127.
    }
  
    geek &gt;&gt; outputHexString;
  
    cout &lt;&lt; "input hex string:  " &lt;&lt; inputHexString&lt;&lt;endl;
    cout &lt;&lt; "output hex string: " &lt;&lt; outputHexString&lt;&lt;endl;
    if(0 == inputHexString.compare(outputHexString))
      cout&lt;&lt;"hex encoding successful"&lt;&lt;endl;
    else
      cout&lt;&lt;"hex encoding failed"&lt;&lt;endl;
  
    if(NULL != hexBuffer)
        delete[] hexBuffer;
  
    return 0;
  }
  
  // output
  // can some one explain ? I am sure its something silly that I am missing.
  </code></pre>