I have this code:
string get_md5sum(unsigned char* md) {
char buf[MD5_DIGEST_LENGTH + MD5_DIGEST_LENGTH];
char *bptr;
bptr = buf;
for(int i = 0; i < MD5_DIGEST_LENGTH; i++) {
bptr += sprintf(bptr, "%02x", md[i]);
}
bptr += '\0';
string x(buf);
return x;
}
Unfortunately, this is some C combined with some C++. It does compile, but I don’t like the printf and char*’s. I always thought this was not necessary in C++, and that there were other functions and classes to realize this. However, I don’t completely understand what is going on with this:
bptr += sprintf(bptr, "%02x", md[i]);
And therefore I don’t know how to convert it into C++. Can someone help me out with that?
sprintfreturns number of bytes written. So this one writes tobptrtwo bytes (value ofmd[i]converted to%02x-> which means hex, padded on 2 chars with zeroes from left), and increasesbptrby number of bytes written, so it points on string’s (buf) end.I don’t get the
bptr += '\0';line, IMO it should be*bptr = '\0';in C++ it should be written like this:
EDIT: updated my c++ answer