void WriteLog_login(const unsigned char* data, int size)
{
HANDLE hLogFile = CreateFileA("login.log", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
unsigned char buf[3] = { 0 };
DWORD dwBytes = 0;
int err = 0;
int i = 0;
for(; i < size; i++ ) {
sprintf((char*)buf, "%02x", data[i]);
if ( WriteFile(hLogFile, buf, 2, &dwBytes, NULL) == FALSE ) {
err = GetLastError();
break;
}
if ( i != 0 && i % 16 == 0 ) {
WriteFile(hLogFile, "\r\n", 4, &dwBytes, NULL);
}
}
CloseHandle(hLogFile);
}
注意:必须是unsigned char,若是char则很容易溢出,破坏堆栈
另附一个小玩意:
unsigned char a[4] = { 0x0 };
a[0] = 0x00;
a[1] = 0x00;
a[2] = 0x00;
a[3] = 0x01;
int v = *(int*)&a;