 <pre><code>/*! * reads binary data into the string. * @status : OK. */ class UReadBinaryString { static std::string read(std::istream &amp;is, uint32_t size) { std::string returnStr; if(size &gt; 0) { CWrapPtr&lt;char&gt; buff(new char[size]); // custom smart pointer is.read(reinterpret_cast&lt;char*&gt;(buff.m_obj), size); returnStr.assign(buff.m_obj, size); } return returnStr; } }; class objHeader { public: std::string m_ID; // serialize std::ostream &amp;operator &lt;&lt; (std::ostream &amp;os) { uint32_t size = (m_ID.length()); os.write(reinterpret_cast&lt;char*&gt;(&amp;size), sizeof(uint32_t)); os.write(m_ID.c_str(), size); return os; } // de-serialize std::istream &amp;operator &gt;&gt; (std::istream &amp;is) { uint32_t size; is.read(reinterpret_cast&lt;char*&gt;(&amp;size), sizeof(uint32_t)); m_ID = UReadBinaryString::read(is, size); return is; } }; </code></pre>