I'd like to use a certain md5-class. It reads a filestream into an unsigned char array, like this:
That code won't compile in Visual C++, because stream.read only accepts a char* and not a unsigned char* as a first parameter.Code:void MD5::update(ifstream& stream) { unsigned char buffer[1024]; while (stream.good()) { stream.read(buffer, 1024); update(buffer, stream.gcount()); } }
I noticed that the microsoft implementation uses this:
So i created this typedef:Code:typedef basic_ifstream<char, char_traits<char> > ifstream;
Using 'ifucstream' does compile, but somehow the stream fails after a few loops.Code:typedef basic_ifstream<unsigned char, char_traits<unsigned char> > ifucstream;
So then i tried using reinterpret_cast to cast buffer as a char* and i also tried 'C'-casting:
Both work fine. However, i've read somewhere that these kind of typecasts are dangerous. Is that true in this case? And does anyone know why my typedef solution doesn't work? And finally, could using reinterpret_cast or 'C'-casting (like above) make the hash calculation slower, and which type of casting should i prefer?Code:stream.read(reinterpret_cast<char*>(buffer), 1024); stream.read((char*)buffer, 1024);




Reply With Quote