一些小函数

来源:互联网 发布:网络快车加 编辑:程序博客网 时间:2024/05/22 04:25
//获取当前时间函数
string getnowtime(int type) {
time_t t = time(0);
char tmp[64];
if (type == 1) {
strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S", localtime(&t));
} else {
strftime(tmp, sizeof(tmp), "%Y-%m-%d", localtime(&t));
}
string mytime(tmp);
return mytime;
}

//求最小值函数,临时
int MIN(int a, int b) {
return a >= b ? b : a;
}

//MD5加密函数

char *MD5String(const char *string) {
int i;
MD5_CTX context;
unsigned char digest[16];
char *result = (char *) malloc(33);

MD5_Init(&context);
MD5_Update(&context, string, strlen(string));
MD5_Final(digest, &context);

for (i = 0; i < 16; i++)
sprintf(result + 2 * i, "%02x", digest[i]);
result[32] = 0;
return result;
}

//int 转 string函数
string getstring(const int n) {
std::stringstream newstr;
newstr << n;
return newstr.str();
}

//返回一个随机数函数
int getrandstr() {
int t = rand() % 100 + 1;
return t;
}

//获得本次产生的日志名函数
string getlogname() {
string dname("LOG/");
string tname = getnowtime(2);
string fname("_chat.log");
string name = dname + tname + fname;
return name;
}

原创粉丝点击