#include 赞 踩 #include "stdafx.h" inline int fromHex(char c) inline int toHex(unsigned int c) bool ri = true; return init; BYTE vtByte[16] = {0}; Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。
时间戳校验和计算
#include <iostream>
#include <vector>
#include <time.h>
#include <afxtempl.h>
using namespace std;
{
return ((c >= '0') && (c <= '9')) ? int(c - '0') : (((c >= 'A') && (c <= 'F')) ? int(c - 'A' + 10) : (((c >= 'a') && (c <= 'f')) ? int(c - 'a' + 10) : -1));
}
{
return ((c >= 0) && (c <= 9)) ? int(c + '0') : (((c >= 10) && (c <= 15)) ? int(c + 'a'-10) : -1);
}
string FromHex(string data)
{
string init = string((data.size() + 1) / 2, 0);
unsigned char* result = (unsigned char*)init.data() + init.size();
for (int i = data.size() - 1; i >= 0; --i)
{
unsigned char ch = data.at(i);
int tmp = fromHex(ch);
if (-1 == tmp)
{
continue;
}
if (ri) {
--result;
*result = tmp;
ri = false;
}
else
{
*result |= tmp << 4;
ri = true;
}
}
}
string ToHex(string data)
{
string init;
for (int i = 0; i < data.size(); ++i)
{
unsigned char aa = data[i];
//cout << aa << endl;
unsigned char bb = toHex(aa >> 4 & 0xf);
unsigned char cc = toHex(aa & 0xf);
//cout << bb << cc << endl;
init.push_back(bb);
init.push_back(cc);
}
return init;
}
int _tmain(int argc, _TCHAR* argv[])
{
time_t t;
::time(&t);
char szFormat[MAX_PATH] = "";
_snprintf_s(szFormat, MAX_PATH, MAX_PATH, "%ld", (long)t);
int nSize = strlen(szFormat);
vtByte[0] = (nSize + 4);
vtByte[1] = 0x00;
for (int m = 0; m < nSize; m++)
{
vtByte[m+2] = szFormat[m];
}
// 验证校验位
unsigned int nSum = 0;
BYTE szCommand[MAX_PATH] = "";
int nLoop = nSize + 2;
for (int i = 0; i < nLoop; i++)
{
nSum += (unsigned char)vtByte[i];
szCommand[i] = vtByte[i];
}
char szNum[MAX_PATH] = "";
_snprintf_s(szNum, MAX_PATH, MAX_PATH, "%04x", nSum);
for (int i = 0; i < 4; i = i + 2)
{
int n1 = fromHex(szNum[i]);
int n2 = fromHex(szNum[i+1]);
vtByte[nLoop] = n1 * 16 + n2;
nLoop++;
}
// char szNum[MAX_PATH] = "";
// _snprintf_s(szNum, MAX_PATH, MAX_PATH, "%04x", nSum);
// BYTE szTmp[MAX_PATH] = "";
// for (int i = 0; i < strlen(szNum); i++)
// {
// szTmp[i] = szNum[i];
// }
// BYTE vtEnd[4] = {0};
// if (strlen(szNum) <= 4)
// {
// int i = 0;
// for (i; i < 4 - strlen(szNum); i++)
// {
// vtEnd[i] = '0';
// }
// for (int j = 0; j < 4 - i; j++)
// {
// vtEnd[i + j] = szTmp[j];
// }
//
// }
// else
// {
// for (int i = strlen(szNum) - 4, j = 0; i < strlen(szNum); i++, j++)
// {
// vtEnd[j] = szTmp[i];
// }
// }
//
// for (int i = 0; i < sizeof(vtEnd); i++)
// {
// vtByte[i + nLoop] = vtEnd[i];
// }
return 0;
}