publicFunction.h
在这里插入代码片
#ifndef PUBLICFUNCTION_H
#define PUBLICFUNCTION_H
#include <QObject>
struct FuWuQiData{
char id[128];
char datafwq[1024];
FuWuQiData()
{
memset(id,0x00,128);
memset(datafwq,0x00,1024);
}
};//m_FuWuQiData
using namespace std;
struct shouHouWeiXiu{
QString FAC_ID;
QString MIDDLE_CODE;
QString SERIAL_NO;
QString FAULT_CLASS;
QString FAULT_CAUSE;
QString RETEST_FAILURE;
QString FAULT_NUM;
QString FAULT_LOCATION;
QString REWORK_REP_TIMES;
QString ABILITY;
QString PROCESSING_METHOD;
QString TESTER_CODE;
QString MAINTENANCE_PER_CODE;
QString STATE;
QString INPUT_TIME;
QString COMPLEITION_TIME;
QString MAINT_DETAILS;
};
#define ToStr(x) #x
const QString normalStyle = ToStr(font-family:"微软雅黑"; font-size:30px; color: rgb(255,0,0););
const QString InStyle = ToStr(font-family:"微软雅黑"; font-size: 15px; color: rgb(0, 0, 255););
//extern QVector<shouHouWeiXiu> g_vectshouHouWeiXiu; //接收
//extern list<FuWuQiData> g_vectRecPingTai; //接收
#endif // PUBLICFUNCTION_H
// PublicFunction.h: interface for the CPublicFunction class.
//
//
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "afxcoll.h"
#include "../3des.h"
class CPublicFunction
{
public:
bool NoteLog(CString sFile,byte* byData,DWORD dwSize,bool bDel = false);
bool NoteBinLog(CString sFile,byte* byData,DWORD dwSize,bool bDel = false);
bool NoteTxtLog(CString sFile,CString byData,DWORD dwSize,bool bDel,bool isDate);
bool NoteTxtAddLog(CString sFile,CString byData,DWORD dwSize,bool bDel,bool isDate);//往文本里面添加数据
CString Decrypt(CString S);
CString Encrypt(CString S);
char* GetChecksumKey(char *strKEKFront);
unsigned long GenerateCRC32(unsigned char *DataBuf,unsigned long len);
CString GetCurDir();
char* translateCharToUTF_8(char *xmlStream, int len);
char* UnicodeToUTF_8(WCHAR *UniChar);
WCHAR* Gb2312ToUnicode(char *gbBuffer);
char* UnicodeToGB2312(unsigned short uData);
WCHAR* UTF_8ToUnicode(char *ustart);
int BinToInt(CString string);
CString BinToHex(CString BinString);
CString HexToBin(CString string);
WCHAR* UTF8ToUnicode(CString UTF8CString, WCHAR* UnicodeArray, int Length); //UTF8转Unicode
CString UnicodeToGB232(WCHAR* UnicodeArray, int Length); //Unicode转GB232
CString UTF8ToGB232(CString UTF8CString); //UTF8转GB232(通过UTF8->Unicode->GB232)
CString UnicodeToUTF8(WCHAR* UnicodeArray, int Length); //Unicode转UTF8
WCHAR* GB232ToUnicode(CString GB232CString, WCHAR* UnicodeArray, int Length); //GB232转Unicode
CString GB232ToUTF8(CString GB232CString);
CString HexStringOperation(CString sHex);
int CompareHexString(CString sStrng1,CString sStrng2);
bool IsNumber(char* sString,UINT nType);
bool IsNumber(CString sString,UINT nType);
bool ApartString(CString sString,CString sSeparator,CStringArray &group);
virtual bool CheckLastByteIsGB232(byte* byData,DWORD dwDataLen);
virtual char GetMapBYTECode(BYTE byIn);
virtual BYTE GetMapCharCode(char chIn);
virtual void BinToChar(BYTE *byIn, int nLen, char *chOut);
virtual int CharToBin(char *chIn, BYTE *byOut);
virtual DWORD ChangeByteToBin(BYTE *pByData, BYTE nNum);
virtual void ChangeBinToByte(DWORD dwIn, int nNum, BYTE *byOut);
virtual int gsmEncode7bit(const char* pSrc, unsigned char* pDst, int nSrcLength);
virtual int gsmDecode7bit(const unsigned char* pSrc, char* pDst, int nSrcLength);
virtual int CStringToInt(CString sIn);
virtual int HexCStringToInt(CString sHex);
virtual CString IntToChar(int nIn,int nLen,int nHex);
virtual void IntToChar(int nIn, char *chOut, int nLen, int nHex);
virtual void BCDToAcs(byte *pIn, DWORD dwInSize, char *pOut, bool bReversal = true);
virtual void AcsToBCD(char* pIn,byte* pOut,bool bReversal = true);
virtual void CPublicFunction::HighToLow(WORD &wTemp);
CPublicFunction();
virtual ~CPublicFunction();
};
publicfuntion.cpp
#include "publicfunction.h"
//list<FuWuQiData> g_vectRecPingTai; //接收平台信息
//QVector <shouHouWeiXiu> g_vectshouHouWeiXiu; //接收
// PublicFunction.cpp: implementation of the CPublicFunction class.
//
//
#include "stdafx.h"
#include "PublicFunction.h"
#include <stdio.h>
#define OK 0
#define ERROR (-1)
#define UNS_32_BITS unsigned long
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//
// Construction/Destruction
//
const BYTE KEY[]={0xA1,0x34,0x5C,0xA9};
UNS_32_BITS Crc32Table[256]=
{
0x00000000,0x04C11DB7,0x09823B6E,0x0D4326D9,0x130476DC,0x17C56B6B,0x1A864DB2,0x1E475005,
0x2608EDB8,0x22C9F00F,0x2F8AD6D6,0x2B4BCB61,0x350C9B64,0x31CD86D3,0x3C8EA00A,0x384FBDBD,
0x4C11DB70,0x48D0C6C7,0x4593E01E,0x4152FDA9,0x5F15ADAC,0x5BD4B01B,0x569796C2,0x52568B75,
0x6A1936C8,0x6ED82B7F,0x639B0DA6,0x675A1011,0x791D4014,0x7DDC5DA3,0x709F7B7A,0x745E66CD,
0x9823B6E0,0x9CE2AB57,0x91A18D8E,0x95609039,0x8B27C03C,0x8FE6DD8B,0x82A5FB52,0x8664E6E5,
0xBE2B5B58,0xBAEA46EF,0xB7A96036,0xB3687D81,0xAD2F2D84,0xA9EE3033,0xA4AD16EA,0xA06C0B5D,
0xD4326D90,0xD0F37027,0xDDB056FE,0xD9714B49,0xC7361B4C,0xC3F706FB,0xCEB42022,0xCA753D95,
0xF23A8028,0xF6FB9D9F,0xFBB8BB46,0xFF79A6F1,0xE13EF6F4,0xE5FFEB43,0xE8BCCD9A,0xEC7DD02D,
0x34867077,0x30476DC0,0x3D044B19,0x39C556AE,0x278206AB,0x23431B1C,0x2E003DC5,0x2AC12072,
0x128E9DCF,0x164F8078,0x1B0CA6A1,0x1FCDBB16,0x018AEB13,0x054BF6A4,0x0808D07D,0x0CC9CDCA,
0x7897AB07,0x7C56B6B0,0x71159069,0x75D48DDE,0x6B93DDDB,0x6F52C06C,0x6211E6B5,0x66D0FB02,
0x5E9F46BF,0x5A5E5B08,0x571D7DD1,0x53DC6066,0x4D9B3063,0x495A2DD4,0x44190B0D,0x40D816BA,
0xACA5C697,0xA864DB20,0xA527FDF9,0xA1E6E04E,0xBFA1B04B,0xBB60ADFC,0xB6238B25,0xB2E29692,
0x8AAD2B2F,0x8E6C3698,0x832F1041,0x87EE0DF6,0x99A95DF3,0x9D684044,0x902B669D,0x94EA7B2A,
0xE0B41DE7,0xE4750050,0xE9362689,0xEDF73B3E,0xF3B06B3B,0xF771768C,0xFA325055,0xFEF34DE2,
0xC6BCF05F,0xC27DEDE8,0xCF3ECB31,0xCBFFD686,0xD5B88683,0xD1799B34,0xDC3ABDED,0xD8FBA05A,
0x690CE0EE,0x6DCDFD59,0x608EDB80,0x644FC637,0x7A089632,0x7EC98B85,0x738AAD5C,0x774BB0EB,
0x4F040D56,0x4BC510E1,0x46863638,0x42472B8F,0x5C007B8A,0x58C1663D,0x558240E4,0x51435D53,
0x251D3B9E,0x21DC2629,0x2C9F00F0,0x285E1D47,0x36194D42,0x32D850F5,0x3F9B762C,0x3B5A6B9B,
0x0315D626,0x07D4CB91,0x0A97ED48,0x0E56F0FF,0x1011A0FA,0x14D0BD4D,0x19939B94,0x1D528623,
0xF12F560E,0xF5EE4BB9,0xF8AD6D60,0xFC6C70D7,0xE22B20D2,0xE6EA3D65,0xEBA91BBC,0xEF68060B,
0xD727BBB6,0xD3E6A601,0xDEA580D8,0xDA649D6F,0xC423CD6A,0xC0E2D0DD,0xCDA1F604,0xC960EBB3,
0xBD3E8D7E,0xB9FF90C9,0xB4BCB610,0xB07DABA7,0xAE3AFBA2,0xAAFBE615,0xA7B8C0CC,0xA379DD7B,
0x9B3660C6,0x9FF77D71,0x92B45BA8,0x9675461F,0x8832161A,0x8CF30BAD,0x81B02D74,0x857130C3,
0x5D8A9099,0x594B8D2E,0x5408ABF7,0x50C9B640,0x4E8EE645,0x4A4FFBF2,0x470CDD2B,0x43CDC09C,
0x7B827D21,0x7F436096,0x7200464F,0x76C15BF8,0x68860BFD,0x6C47164A,0x61043093,0x65C52D24,
0x119B4BE9,0x155A565E,0x18197087,0x1CD86D30,0x029F3D35,0x065E2082,0x0B1D065B,0x0FDC1BEC,
0x3793A651,0x3352BBE6,0x3E119D3F,0x3AD08088,0x2497D08D,0x2056CD3A,0x2D15EBE3,0x29D4F654,
0xC5A92679,0xC1683BCE,0xCC2B1D17,0xC8EA00A0,0xD6AD50A5,0xD26C4D12,0xDF2F6BCB,0xDBEE767C,
0xE3A1CBC1,0xE760D676,0xEA23F0AF,0xEEE2ED18,0xF0A5BD1D,0xF464A0AA,0xF9278673,0xFDE69BC4,
0x89B8FD09,0x8D79E0BE,0x803AC667,0x84FBDBD0,0x9ABC8BD5,0x9E7D9662,0x933EB0BB,0x97FFAD0C,
0xAFB010B1,0xAB710D06,0xA6322BDF,0xA2F33668,0xBCB4666D,0xB8757BDA,0xB5365D03,0xB1F740B4
};
CPublicFunction::CPublicFunction()
{
}
CPublicFunction::~CPublicFunction()
{
}
//将数字Asc码数据转换成压缩BCD数据
//bReversal:true颠倒数据
void CPublicFunction::AcsToBCD(char *pIn, byte *pOut, bool bReversal)
{
for (UINT i = 0,j = 0; i < strlen(pIn); i+=2,j++)
{
if (bReversal) i ++;
if (i >= strlen(pIn))
{
pOut[j] = 0xf0;
}
else
{
if (pIn[i] >= '0' && pIn[i] <= '9')
{
pOut[j] = (pIn[i] - 0x30)*16;
}
else if (pIn[i] == 'A' || pIn[i] == 'a')
{
pOut[j] = 0xa0;
}
else if (pIn[i] == 'B' || pIn[i] == 'b')
{
pOut[j] = 0xb0;
}
else if (pIn[i] == 'C' || pIn[i] == 'c')
{
pOut[j] = 0xc0;
}
else if (pIn[i] == 'D' || pIn[i] == 'd')
{
pOut[j] = 0xd0;
}
else if (pIn[i] == 'E' || pIn[i] == 'e')
{
pOut[j] = 0xe0;
}
else if (pIn[i] == 'F' || pIn[i] == 'f')
{
pOut[j] = 0xf0;
}
}
if (bReversal)
{
i --;
}
else
{
i ++;
}
if (i >= strlen(pIn))
{
pOut[j] = 0xf0;
}
else
{
if (pIn[i] >= '0' && pIn[i] <= '9')
{
pOut[j] += pIn[i] - 0x30;
}
else if (pIn[i] == 'A' || pIn[i] == 'a')
{
pOut[j] += 0x0a;
}
else if (pIn[i] == 'B' || pIn[i] == 'b')
{
pOut[j] += 0x0b;
}
else if (pIn[i] == 'C' || pIn[i] == 'c')
{
pOut[j] += 0x0c;
}
else if (pIn[i] == 'D' || pIn[i] == 'd')
{
pOut[j] += 0x0d;
}
else if (pIn[i] == 'E' || pIn[i] == 'e')
{
pOut[j] += 0x0e;
}
else if (pIn[i] == 'F' || pIn[i] == 'f')
{
pOut[j] += 0x0f;
}
}
if (! bReversal) i --;
}
}
//对换一个word字的高半字节和低半字节
void CPublicFunction::HighToLow(WORD &wTemp)
{
WORD temp;
temp = wTemp;
wTemp = 0;
wTemp = temp << 8 | temp >>8 ;
}
//将压缩BCD数据转换成Asc码数据
//bReversal:true颠倒数据
void CPublicFunction::BCDToAcs(byte *pIn, DWORD dwInSize, char *pOut, bool bReversal)
{
byte by1,by2;
for (UINT i = 0,j = 0; i < dwInSize; i++,j+=2)
{
by1 = pIn[i]/16;
by2 = pIn[i] & 0x0f;
if (by1 >= 0 && by1 <= 9)
{
pOut[j] = by1 + 0x30;
}
else if (by1 == 0x0a)
{
pOut[j] = 'A';
}
else if (by1 == 0x0b)
{
pOut[j] = 'B';
}
else if (by1 == 0x0C)
{
pOut[j] = 'C';
}
else if (by1 == 0x0D)
{
pOut[j] = 'D';
}
else if (by1 == 0x0E)
{
pOut[j] = 'E';
}
else if (by1 == 0x0F)
{
pOut[j] = 'F';
}
if (by2 >= 0 && by2 <= 9)
{
pOut[j+1] = by2 + 0x30;
}
else if (by2 == 0x0a)
{
pOut[j+1] = 'A';
}
else if (by2 == 0x0b)
{
pOut[j+1] = 'B';
}
else if (by2 == 0x0C)
{
pOut[j+1] = 'C';
}
else if (by2 == 0x0D)
{
pOut[j+1] = 'D';
}
else if (by2 == 0x0E)
{
pOut[j+1] = 'E';
}
else if (by2 == 0x0F)
{
pOut[j+1] = 'F';
}
}
}
//将输入的int型数据转换为指定长度char型数据,
//nLen为指定的长度,如果不足,前面补0,nHex为进制设定,如果超过,后面截去
void CPublicFunction::IntToChar(int nIn, char *chOut, int nLen, int nHex)
{
char chTemp[15];
ZeroMemory(chTemp,15);
itoa(nIn,chTemp,nHex);
int nTemp=strlen(chTemp);
if(nLen>nTemp)
{
for(int i=0;i<nLen-nTemp;i++)
chOut[i]='0';
strcpy(chOut+i,chTemp);
}
else if (nLen == 0)
{
strncpy(chOut,chTemp,nTemp);
chOut[nTemp]=0;
}
else
{
strncpy(chOut,chTemp,nLen);
chOut[nLen]=0;
}
}
CString CPublicFunction::IntToChar(int nIn, int nLen, int nHex)
{
char chTemp[15] = {0};
IntToChar(nIn,chTemp,nLen,nHex);
return chTemp;
}
int CPublicFunction::HexCStringToInt(CString sHex)
{
switch(sHex.GetAt(0))
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'a':
case 'A':
return 10;
case 'b':
case 'B':
return 11;
case 'c':
case 'C':
return 12;
case 'd':
case 'D':
return 13;
case 'e':
case 'E':
return 14;
case 'F':
case 'f':
return 15;
default:
return 0;
}
}
int CPublicFunction::CStringToInt(CString sIn)
{
int nRet = 0;
for(int i = 0;i < sIn.GetLength();i++)
{
nRet = nRet * 16 + HexCStringToInt(sIn.Mid(i,1));
}
return nRet;
}
// 7-bit编码
// pSrc: 源字符串指针
// pDst: 目标编码串指针
// nSrcLength: 源字符串长度
// 返回: 目标编码串长度
int CPublicFunction::gsmEncode7bit(const char* pSrc, unsigned char* pDst, int nSrcLength)
{
int nSrc; // 源字符串的计数值
int nDst; // 目标编码串的计数值
int nChar; // 当前正在处理的组内字符字节的序号,范围是0-7
unsigned char nLeft; // 上一字节残余的数据
// 计数值初始化
nSrc = 0;
nDst = 0;
// 将源串每8个字节分为一组,压缩成7个字节
// 循环该处理过程,直至源串被处理完
// 如果分组不到8字节,也能正确处理
while(nSrc<nSrcLength)
{
// 取源字符串的计数值的最低3位
nChar = nSrc & 7;
// 处理源串的每个字节
if(nChar == 0)
{
// 组内第一个字节,只是保存起来,待处理下一个字节时使用
nLeft = *pSrc;
}
else
{
// 组内其它字节,将其右边部分与残余数据相加,得到一个目标编码字节
*pDst = (*pSrc << (8-nChar)) | nLeft;
*pDst++; //我加的,原有程序有误
// 将该字节剩下的左边部分,作为残余数据保存起来
nLeft = *pSrc >> nChar;
// 修改目标串的指针和计数值 pDst++;
nDst++;
}
// 修改源串的指针和计数值
pSrc++; nSrc++;
}
// 返回目标串长度
return nDst;
}
// 7-bit解码
// pSrc: 源编码串指针
// pDst: 目标字符串指针
// nSrcLength: 源编码串长度
// 返回: 目标字符串长度
int CPublicFunction::gsmDecode7bit(const unsigned char* pSrc, char* pDst, int nSrcLength)
{
int nSrc; // 源字符串的计数值
int nDst; // 目标解码串的计数值
int nByte; // 当前正在处理的组内字节的序号,范围是0-6
unsigned char nLeft; // 上一字节残余的数据
// 计数值初始化
nSrc = 0;
nDst = 0;
// 组内字节序号和残余数据初始化
nByte = 0;
nLeft = 0;
// 将源数据每7个字节分为一组,解压缩成8个字节
// 循环该处理过程,直至源数据被处理完
// 如果分组不到7字节,也能正确处理
while(nSrc<nSrcLength)
{
// 将源字节右边部分与残余数据相加,去掉最高位,得到一个目标解码字节
*pDst = ((*pSrc << nByte) | nLeft) & 0x7f;
// 将该字节剩下的左边部分,作为残余数据保存起来
nLeft = *pSrc >> (7-nByte);
// 修改目标串的指针和计数值
pDst++;
nDst++;
// 修改字节计数值
nByte++;
// 到了一组的最后一个字节
if(nByte == 7)
{
// 额外得到一个目标解码字节
*pDst = nLeft;
// 修改目标串的指针和计数值
pDst++;
nDst++;
// 组内字节序号和残余数据初始化
nByte = 0;
nLeft = 0;
}
// 修改源串的指针和计数值
pSrc++;
nSrc++;
}
*pDst = 0;
// 返回目标串长度
return nDst;
}
//将输入的二进制数据转换为BYTE 数据,nNum指的是要转换的字节个数
void CPublicFunction::ChangeBinToByte(DWORD dwIn, int nNum, BYTE *byOut)
{
WORD wTm;
ASSERT(nNum>0);
ASSERT(nNum<=4);
switch(nNum)
{
case 1:
byOut[0]=(BYTE)dwIn;
break;
case 2:
wTm=LOWORD(dwIn);
byOut[0]=HIBYTE(wTm);
byOut[1]=LOBYTE(wTm);
break;
case 3:
wTm=HIWORD(dwIn);
byOut[0]=LOBYTE(wTm);
wTm=LOWORD(dwIn);
byOut[1]=HIBYTE(wTm);
byOut[2]=LOBYTE(wTm);
break;
case 4:
wTm=HIWORD(dwIn);
byOut[0]=HIBYTE(wTm);
byOut[1]=LOBYTE(wTm);
wTm=LOWORD(dwIn);
byOut[2]=HIBYTE(wTm);
byOut[3]=LOBYTE(wTm);
break;
default:
break;
}
}
/*将byte类型的数据nNum个字节换为二进制数据
默认为将4个字节的数据转换为DWORD类型的数据
*/
DWORD CPublicFunction::ChangeByteToBin(BYTE *pByData, BYTE nNum)
{
DWORD dwRtn;
WORD wTml,wTmh;
switch(nNum)
{
case 1:
dwRtn=pByData[0];
break;
case 2:
wTmh=MAKEWORD(pByData[1],pByData[0]);
dwRtn=wTmh;
break;
case 3:
wTmh=pByData[0];
wTml=MAKEWORD(pByData[2],pByData[1]);
dwRtn=MAKELONG(wTml,wTmh);
break;
case 4:
wTmh=MAKEWORD(pByData[1],pByData[0]);
wTml=MAKEWORD(pByData[3],pByData[2]);
dwRtn=MAKELONG(wTml,wTmh);
break;
default:
dwRtn=0;
break;
}
return dwRtn;
}
/*
* 将二进制数据转换为字符串信息,注意,要将输入的16进制数据原样显示
注意:作为输入的字符串大小应该足够的长,至少是nLen的长度的两倍以上
*/
void CPublicFunction::BinToChar(BYTE *byIn, int nLen, char *chOut)
{
ASSERT(byIn);
ASSERT(chOut);
BYTE byTm0;
int nEnd=nLen*2;
for(int i=0;i<nEnd;i++)
{
if(i%2==0)
{
byTm0=(byIn[i/2]>>4)&0x0f;
chOut[i]=GetMapBYTECode(byTm0);
}
else
{
byTm0=byIn[i/2]&0x0f;
chOut[i]=GetMapBYTECode(byTm0);
}
}
chOut[nEnd]=0;
}
/*
* 将16进制的字符串信息转换为二进制数据信息,注意,nLen必须为2的倍数
返回值为转换后的个数
*/
int CPublicFunction::CharToBin(char *chIn, BYTE *byOut)
{
int i,len=0;
ASSERT(chIn);
ASSERT(byOut);
BYTE byTemp,byTm;
len=strlen(chIn);
for(i=0;i<len;i++)
{
byTm=GetMapCharCode(chIn[i]);
if(i%2==0) byTemp=(byTm<<4)&0xf0;
else byOut[i/2]=byTemp|byTm;
}
return len/2;
}
//根据映射,将byte转换为字符类型的数据
char CPublicFunction::GetMapBYTECode(BYTE byIn)
{
switch(byIn)
{
case 0:
return '0';
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
case 10:
return 'A';
case 11:
return 'B';
case 12:
return 'C';
case 13:
return 'D';
case 14:
return 'E';
case 15:
return 'F';
default:
return 'N';
}
}
//根据映射,将字符转换为byte类型的数据
BYTE CPublicFunction::GetMapCharCode(char chIn)
{
switch(chIn)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'a':
case 'A':
return 10;
case 'b':
case 'B':
return 11;
case 'c':
case 'C':
return 12;
case 'd':
case 'D':
return 13;
case 'e':
case 'E':
return 14;
case 'F':
case 'f':
return 15;
default:
return 0xff;
}
}
//检查最后一个字节是否是汉字的高半字节
bool CPublicFunction::CheckLastByteIsGB232(byte *byData, DWORD dwDataLen)
{
if (dwDataLen == 0) return false;
if (byData[dwDataLen - 1] <= 0x80) return false;
//判断最后一个字节离最近的不大于0x80的字节的字节数
//如果是偶数个字节,代表最后一个字节非汉字高字节;否则,是高半字节
for(DWORD i = dwDataLen - 1; i >= 0; i--)
{
if (byData[i] <= 0x80)
{
if ((dwDataLen - 1 - i) % 2 == 0)//偶数个
{
return false;
}
else
{
return true;
}
}
}
if (dwDataLen % 2 == 0)
{
return false;
}
else
{
return true;
}
}
//根据分隔符,分解字符串
bool CPublicFunction::ApartString(CString sString,CString sSeparator,CStringArray &group)
{
if (! sString.GetLength() || ! sSeparator.GetLength())
{
return false;
}
int nPos = 0;
bool bFind = false;
nPos = sString.Find(sSeparator);
while (! (nPos < 0))
{
bFind = true;
group.Add(sString.Left(nPos));
sString = sString.Right(sString.GetLength() - nPos - sSeparator.GetLength());
nPos = sString.Find(sSeparator);
}
if (sString.GetLength())
{
group.Add(sString);
}
return bFind;
}
//判断字符串是否是全是数字. nType:0代表存数字; 1代表16进制数字
bool CPublicFunction::IsNumber(CString sString,UINT nType)
{
return IsNumber(sString.GetBuffer(0),nType);
}
//判断字符串是否是全是数字. nType:0代表存数字; 1代表16进制数字
bool CPublicFunction::IsNumber(char* sString,UINT nType)
{
bool bDecimal = false;
for(UINT i = 0; i < strlen(sString); i++)
{
if (! (sString[i] >= 0x30 && sString[i] <= 0x39))
{
if (i == 0)
{
if (sString[i] == 0x2B || sString[i] == 0x2D) continue;//首位负号或正号
if (sString[i] == 0x2E) return false;//首位为小数点
}
else
{
if (sString[i] == 0x2E)//小数点
{
if (bDecimal) return false;//已存在小数点
if (i == strlen(sString) - 1) return false;//最后一位是小数点
bDecimal = true;
continue;
}
}
switch(nType)
{
case 0://存数字
return false;
break;
case 1://16进制字符串
if (!((sString[i] >= 0x041 && sString[i] <= 0x46) || (sString[i] >= 0x61 && sString[i] <= 0x66)))
{
return false;
}
break;
}
}
}
return true;
}
//比较两个十六进制字符串大小
//返回1:sString1 大于 sString2
//返加0:sString1 等于 sString2
//返回-1:sString1 小于 sString2
int CPublicFunction::CompareHexString(CString sString1, CString sString2)
{
int i,nMaxLen = max(sString1.GetLength(),sString2.GetLength());
for (i = sString1.GetLength(); i < nMaxLen; i++)
{
sString1 = "0" + sString1;
}
for (i = sString2.GetLength(); i < nMaxLen; i++)
{
sString2 = "0" + sString2;
}
for (i = 0; i < nMaxLen; i++)
{
if ((byte)sString1.GetAt(i) > (byte)sString2.GetAt(i))
{
return 1;
}
else if ((byte)sString1.GetAt(i) < (byte)sString2.GetAt(i))
{
return -1;
}
}
return 0;
}
//十六进制字符串运算,现在只支持递增
CString CPublicFunction::HexStringOperation(CString sHex)
{
CString sRet = "",sTemp;
bool bContinue = false;
for(int i = sHex.GetLength() - 1; i >=0; i--)
{
bContinue = false;
sTemp = sHex.Mid(i,1);
sTemp.MakeUpper();
if (sTemp == "9")
{
sRet = "A" + sRet;
}
else if(sTemp == "A")
{
sRet = "B" + sRet;
}
else if(sTemp == "B")
{
sRet = "C" + sRet;
}
else if(sTemp == "C")
{
sRet = "D" + sRet;
}
else if(sTemp == "D")
{
sRet = "E" + sRet;
}
else if(sTemp == "E")
{
sRet = "F" + sRet;
}
else if(sTemp == "F")
{
sRet = "0" + sRet;
bContinue = true;
}
else
{
sRet = IntToChar(atoi(sTemp) + 1,0,10) + sRet;
}
if (! bContinue)
{
if (i)
{
sRet = sHex.Left(i) + sRet;
}
break;
}
}
if (bContinue)
{
sRet = "1" + sRet;
}
return sRet;
}
WCHAR* CPublicFunction::UTF8ToUnicode(CString UTF8CString, WCHAR* UnicodeArray, int Length)
{
int UArrayLength=0;
//对UTF8CString里的字符逐个判断
for (int aaa=0; aaa<UTF8CString.GetLength() && UArrayLength<Length-1; aaa++)
{
if ((UTF8CString.GetAt(aaa) & 0X80) == 0X00) //如果是标准Ascii字符,直接入值
UnicodeArray[UArrayLength++] = UTF8CString.GetAt(aaa);
else
if ((UTF8CString.GetAt(aaa) & 0XE0) == 0XC0) //如果是110XXXXX 10XXXXXX形式的UTF-8字符
{
if (aaa+1 >= UTF8CString.GetLength())
{
AfxMessageBox("UTF8ToUnicode Error : Invalid UTF-8 String");
break;
}
UnicodeArray[UArrayLength++] = (UTF8CString.GetAt(aaa) & 0X1F) * 256/4 + (UTF8CString.GetAt(aaa+1) & 0X3F);
aaa++;
}
else
if ((UTF8CString.GetAt(aaa) & 0XF0) == 0XE0) //如果是1110XXXX 10XXXXXX 10XXXXXX形式的UTF-8字符
{
if (aaa+2 >= UTF8CString.GetLength())
{
AfxMessageBox("UTF8ToUnicode Error : Invalid UTF-8 String");
break;
}
UnicodeArray[UArrayLength++] = (UTF8CString.GetAt(aaa) & 0X0F) * 256*16 +
(UTF8CString.GetAt(aaa+1) & 0X3F) * 256/4 + (UTF8CString.GetAt(aaa+2) & 0X3F);
aaa += 2;
}
else
{
AfxMessageBox("UTF8ToUnicode Limit : Can't do with UTF8ToLongUnicode");
break;
}
}
UnicodeArray[UArrayLength] = 0X0000;
return UnicodeArray;
}
CString CPublicFunction::UnicodeToGB232(WCHAR* UnicodeArray, int Length)
{
CString ResultCString;
char *TempChar = new char[Length*2];
for (int aaa=0; aaa<Length; aaa++)
if (UnicodeArray[aaa] == 0) Length = aaa;
TempChar[WideCharToMultiByte(CP_ACP,NULL,UnicodeArray,Length,TempChar,Length*2,NULL,NULL)] = 0X00;
ResultCString = CString(TempChar);
delete TempChar;
return ResultCString;
}
CString CPublicFunction::UTF8ToGB232(CString UTF8CString)
{
WCHAR *TempUnicodeArray = new WCHAR[UTF8CString.GetLength()+1];
UTF8ToUnicode(UTF8CString,TempUnicodeArray,UTF8CString.GetLength()+1);
return UnicodeToGB232(TempUnicodeArray,UTF8CString.GetLength()+1);
}
CString CPublicFunction::UnicodeToUTF8(WCHAR* UnicodeArray, int Length)
{
CString ResultCString;
for (int aaa=0; aaa<Length; aaa++)
if (UnicodeArray[aaa] == 0) Length = aaa;
//对UnicodeArray的字符逐个判断
for (aaa=0; aaa<Length; aaa++)
{
if (UnicodeArray[aaa] >= 0X0000 && UnicodeArray[aaa] <= 0X007F) //如果是标准Ascii字符,直接入值
ResultCString += char(UnicodeArray[aaa]);
else
if (UnicodeArray[aaa] >= 0X0080 && UnicodeArray[aaa] <= 0X07FF) //如果是110XXXXX 10XXXXXX形式的UTF-8字符
{
ResultCString += char(0XC0 + UnicodeArray[aaa] / (256/4));
ResultCString += char(0X80 + UnicodeArray[aaa] & 0X3F);
}
else
if (UnicodeArray[aaa] >= 0X0800 && UnicodeArray[aaa] <= 0XFFFF) //如果是1110XXXX 10XXXXXX 10XXXXXX形式的UTF-8字符
{
ResultCString += char(0XE0 + UnicodeArray[aaa] / (256*16));
ResultCString += char(0X80 + UnicodeArray[aaa] % (256*16) / (256/4));
ResultCString += char(0X80 + UnicodeArray[aaa] & 0X003F);
}
else
{
char TempChars[10];
AfxMessageBox(CString("I fount a special Unicode:") + CString(itoa(UnicodeArray[aaa],TempChars,16)));
break;
}
}
return ResultCString;
}
WCHAR* CPublicFunction::GB232ToUnicode(CString GB232CString, WCHAR* UnicodeArray, int Length)
{
UnicodeArray[MultiByteToWideChar(CP_ACP,NULL,GB232CString,GB232CString.GetLength(),UnicodeArray,Length)] = 0X0000;
return UnicodeArray;
}
CString CPublicFunction::GB232ToUTF8(CString GB232CString)
{
WCHAR *TempUnicodeArray = new WCHAR[GB232CString.GetLength()+1];
GB232ToUnicode(GB232CString,TempUnicodeArray,GB232CString.GetLength()+1);
return UnicodeToUTF8(TempUnicodeArray,GB232CString.GetLength()+1);
}
CString CPublicFunction::HexToBin(CString string)//将16进制数转换成2进制
{
if( string == "0") return "0000";
if( string == "1") return "0001";
if( string == "2") return "0010";
if( string == "3") return "0011";
if( string == "4") return "0100";
if( string == "5") return "0101";
if( string == "6") return "0110";
if( string == "7") return "0111";
if( string == "8") return "1000";
if( string == "9") return "1001";
if( string == "a") return "1010";
if( string == "b") return "1011";
if( string == "c") return "1100";
if( string == "d") return "1101";
if( string == "e") return "1110";
if( string == "f") return "1111";
return "";
}
CString CPublicFunction::BinToHex(CString BinString)//将2进制数转换成16进制
{
if( BinString == "0000") return "0";
if( BinString == "0001") return "1";
if( BinString == "0010") return "2";
if( BinString == "0011") return "3";
if( BinString == "0100") return "4";
if( BinString == "0101") return "5";
if( BinString == "0110") return "6";
if( BinString == "0111") return "7";
if( BinString == "1000") return "8";
if( BinString == "1001") return "9";
if( BinString == "1010") return "a";
if( BinString == "1011") return "b";
if( BinString == "1100") return "c";
if( BinString == "1101") return "d";
if( BinString == "1110") return "e";
if( BinString == "1111") return "f";
return "";
}
int CPublicFunction::BinToInt(CString string)//2进制字符数据转换成10进制整型
{
int len =0;
int tempInt = 0;
int strInt = 0;
for(int i =0 ;i < string.GetLength() ;i ++)
{
tempInt = 1;
strInt = (int)string.GetAt(i)-48;
for(int k =0 ;k < 7-i ; k++)
{
tempInt = 2*tempInt;
}
len += tempInt*strInt;
}
return len;
}
WCHAR* CPublicFunction::UTF_8ToUnicode(char *ustart) //把UTF-8转换成Unicode
{
char char_one;
char char_two;
char char_three;
int Hchar;
int Lchar;
char uchar[2];
WCHAR *unicode;
CString string_one;
CString string_two;
CString string_three;
CString combiString;
char_one = *ustart;
char_two = *(ustart+1);
char_three = *(ustart+2);
string_one.Format("%x",char_one);
string_two.Format("%x",char_two);
string_three.Format("%x",char_three);
string_three = string_three.Right(2);
string_two = string_two.Right(2);
string_one = string_one.Right(2);
string_three = HexToBin(string_three.Left(1))+HexToBin(string_three.Right(1));
string_two = HexToBin(string_two.Left(1))+HexToBin(string_two.Right(1));
string_one = HexToBin(string_one.Left(1))+HexToBin(string_one.Right(1));
combiString = string_one +string_two +string_three;
combiString = combiString.Right(20);
combiString.Delete(4,2);
combiString.Delete(10,2);
Hchar = BinToInt(combiString.Left(8));
Lchar = BinToInt(combiString.Right(8));
uchar[1] = (char)Hchar;
uchar[0] = (char)Lchar;
unicode = (WCHAR *)uchar;
return unicode;
}
char* CPublicFunction::UnicodeToGB2312(unsigned short uData) //把Unicode 转换成 GB2312
{
char *buffer ;
buffer = new char[sizeof(WCHAR)];
WideCharToMultiByte(CP_ACP,NULL,&uData,1,buffer,sizeof(WCHAR),NULL,NULL);
return buffer;
}
WCHAR* CPublicFunction::Gb2312ToUnicode(char *gbBuffer) //GB2312 转换成 Unicode
{
WCHAR *uniChar;
uniChar = new WCHAR[1];
::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,uniChar,1);
return uniChar;
}
char* CPublicFunction::UnicodeToUTF_8(WCHAR *UniChar) // Unicode 转换成UTF-8
{
char *buffer;
CString strOne;
CString strTwo;
CString strThree;
CString strFour;
CString strAnd;
buffer = new char[3];
int hInt,lInt;
hInt = (int)((*UniChar)/256);
lInt = (*UniChar)%256;
CString string ;
string.Format("%x",hInt);
strTwo = HexToBin(string.Right(1));
string = string.Left(string.GetLength() - 1);
strOne = HexToBin(string.Right(1));
string.Format("%x",lInt);
strFour = HexToBin(string.Right(1));
string = string.Left(string.GetLength() -1);
strThree = HexToBin(string.Right(1));
strAnd = strOne +strTwo + strThree + strFour;
strAnd.Insert(0,"1110");
strAnd.Insert(8,"10");
strAnd.Insert(16,"10");
strOne = strAnd.Left(8);
strAnd = strAnd.Right(16);
strTwo = strAnd.Left(8);
strThree = strAnd.Right(8);
*buffer = (char)BinToInt(strOne);
buffer[1] = (char)BinToInt(strTwo);
buffer[2] = (char)BinToInt(strThree);
return buffer;
}
char* CPublicFunction::translateCharToUTF_8(char *xmlStream, int len)
{
int newCharLen =0 ;
int oldCharLen = 0;
int revCharLen = len;
char* newCharBuffer = NULL;
char* finalCharBuffer = NULL;
char *buffer = NULL;
CString string;
buffer = new char[sizeof(WCHAR)];
newCharBuffer = new char[int(1.5*revCharLen)];//设置最大的一个缓冲区
while(oldCharLen < revCharLen)
{
if( *(xmlStream + oldCharLen) >= 0)
{
*(newCharBuffer+newCharLen) = *(xmlStream +oldCharLen);
newCharLen ++;
oldCharLen ++;
}//如果是英文直接复制就可以
else
{
WCHAR *pbuffer = this->Gb2312ToUnicode(xmlStream+oldCharLen);
buffer = this->UnicodeToUTF_8(pbuffer);
*(newCharBuffer+newCharLen) = *buffer;
*(newCharBuffer +newCharLen +1) = *(buffer + 1);
*(newCharBuffer +newCharLen +2) = *(buffer + 2);
newCharLen += 3;
oldCharLen += 2;
}
}
newCharBuffer[newCharLen] = '\0';
CString string1 ;
string1.Format("%s",newCharBuffer);
finalCharBuffer = new char[newCharLen+1];
memcpy(finalCharBuffer,newCharBuffer,newCharLen+1);
return finalCharBuffer;
}
CString CPublicFunction::GetCurDir()
{
CString strRet;
char strDir[255];
int intLen = GetModuleFileName(NULL,strDir,255);
strDir[intLen] = 0;
strRet = (CString)strDir;
int intTemp;
int intPos = strRet.Find("\\");
while (intPos > 0)
{
intTemp = intPos;
intPos = strRet.Find("\\",intPos + 1);
}
if (intTemp > 0)
{
strRet = strRet.Mid(0,intTemp + 1);
}
return strRet;
}
bool CPublicFunction::NoteLog(CString sFile, byte *byData, DWORD dwSize, bool bDel)
{
CFile* file = new CFile;
CFileStatus filestatus;
ZeroMemory(&filestatus,sizeof(CFileStatus));
if (!file->GetStatus(sFile,filestatus))
{
file->Open(sFile,file->modeCreate);
file->Close();
}
else
{
if (bDel)
{
file->Open(sFile,file->modeCreate);
file->Close();
}
}
file->Open(sFile,file->modeWrite);
file->SeekToEnd();
CString temp,out = "";
for (DWORD i = 0; i < dwSize; i++)
{
temp.Format("%02X",byData[i]);
if (i == dwSize - 1)
{
out += temp;
}
else
{
out += temp + " ";
}
}
CTime cur = CTime::GetCurrentTime();
out = cur.Format("%Y-%m-%d %H:%M:%S") + " --> " + out;;
file->Write(out,out.GetLength());
file->Write("\r\n",2);
file->Close();
delete file;
return true;
}
bool CPublicFunction::NoteBinLog(CString sFile, byte *byData, DWORD dwSize, bool bDel)
{
CFile* file = new CFile;
CFileStatus filestatus;
ZeroMemory(&filestatus,sizeof(CFileStatus));
if (!file->GetStatus(sFile,filestatus))
{
file->Open(sFile,file->modeCreate);
file->Close();
}
else
{
if (bDel)
{
file->Open(sFile,file->modeCreate);
file->Close();
}
}
file->Open(sFile,file->modeWrite);
file->SeekToEnd();
file->Write(byData,dwSize);
file->Close();
delete file;
return true;
}
bool CPublicFunction::NoteTxtLog(CString sFile,CString byData,DWORD dwSize,bool bDel,bool isDate)
{
CFile* file = new CFile;
CFileStatus filestatus;
ZeroMemory(&filestatus,sizeof(CFileStatus));
if (!file->GetStatus(sFile,filestatus))
{
file->Open(sFile,file->modeCreate);
file->Close();
}
else
{
if (bDel)
{
file->Open(sFile,file->modeCreate);
file->Close();
}
}
file->Open(sFile,file->modeWrite);
file->SeekToEnd();
CTime cur = CTime::GetCurrentTime();
CString out="";
if (isDate)
out = cur.Format("%Y-%m-%d %H:%M:%S") + ":" + byData;
else
out =byData;
file->Write(out,out.GetLength());
file->Write("\r\n",2);
file->Close();
delete file;
return true;
}
bool CPublicFunction::NoteTxtAddLog(CString sFile,CString byData,DWORD dwSize,bool bDel,bool isDate)
{
CFile* file = new CFile;
CFileStatus filestatus;
ZeroMemory(&filestatus,sizeof(CFileStatus));
if (!file->GetStatus(sFile,filestatus))
{
file->Open(sFile,file->modeCreate);
file->Close();
}
// else
// {
// if (bDel)
// {
// file->Open(sFile,file->modeCreate);
// file->Close();
// }
// }
//
// file->Open(sFile,file->modeWrite);
// file->SeekToEnd();
//file->
//添加文件
CStdioFile stfile;
stfile.Open(sFile,CFile::modeWrite);
stfile.GetLength();
stfile.SeekToEnd();
stfile.WriteString(byData+"\n");
stfile.Close();
//
// CTime cur = CTime::GetCurrentTime();
// CString out="";
// if (isDate)
// out = cur.Format("%Y-%m-%d %H:%M:%S") + ":" + byData;
// else
// out =byData;
// file->Write(out,out.GetLength());
// file->Write("\r\n",2);
// file->Close();
// delete file;
return true;
}
CString CPublicFunction::Encrypt(CString S)
{
CString Result,str;
int i,j;
int ikey=0;
BYTE bM;
Result=S;
for(i=0;i<S.GetLength();i++)
{
bM=KEY[ikey]+S.GetAt(i);
if(bM>255)bM-=256;
Result.SetAt(i,bM);
ikey++;
if(ikey>=4)ikey=0;
}
S=Result;
Result.Empty();
for(i=0;i<S.GetLength();i++)
{
j=(BYTE)S.GetAt(i);
str="11";
str.SetAt(0,65+j/26);
str.SetAt(1,65+j%26);
Result+=str;
}
return Result;
}
CString CPublicFunction::Decrypt(CString S)
{
CString Result,str;
int i,j;
int ikey=0;
BYTE bM;
Result.Empty();
for(i=0;i<S.GetLength()/2;i++)
{
j=((BYTE)S.GetAt(2*i)-65)*26;
j+=(BYTE)S.GetAt(2*i+1)-65;
str="1";
str.SetAt(0,j);
Result+=str;
}
S=Result;
for(i=0;i<S.GetLength();i++)
{
bM=S.GetAt(i);
if(bM>=KEY[ikey]){
bM=bM-KEY[ikey];
}else
bM=256+bM-KEY[ikey];
Result.SetAt(i,bM);
ikey++;
if(ikey>=4)ikey=0;
}
return Result;
}
int BCDtoASCII(char *str, int strlen,char *ascii)
{
int i = 0, p = 0;
byte ch;
while(i<strlen)
{
ch = str[i++];
p += sprintf(ascii+p, "%02X", ch);
}
return strlen * 2;
}
char* CPublicFunction::GetChecksumKey(char *strKEKFront)
{
char m_szTermNO[128];
byte KEKOne[20];
byte KEKLast[20];
byte KEK[40];
memset(m_szTermNO, 0, sizeof(m_szTermNO));
memset(KEKOne,0x00,20);
memset(KEK,0,40);
AcsToBCD(strKEKFront,KEKOne,false);
memset(KEKLast,0,20);
Encrypt3DES(KEKLast,KEKOne, 8, 1); //得到校验值
memset(KEK,0,40);
BCDtoASCII((char *)KEKLast,8,(char *)KEK); //此KEK就是CHECKSUM
memcpy(m_szTermNO,KEK,4); //将操作代码返回回去
return m_szTermNO;
}
//CRC16算法
__int16 g_crc16_bin = (__int16)0xFFFF;
__int16 CreateCRC16(char *databuf, DWORD len)
{
DWORD i = 0;
DWORD j;
__int8 data;
while (i < len)
{
data =databuf[i];
for (j = 0; j < 8; j++)
{
BOOL c15 = ((g_crc16_bin >> 15 & 1) == 1);
BOOL bit = ((databuf[i] >> (7 - j) & 1) == 1);
g_crc16_bin <<= 1;
if (c15 ^ bit)
g_crc16_bin ^= 0x1021;
}
i++;
}
return g_crc16_bin;
}
//CRC32算法
unsigned long byte_int(byte *bytes)
{
unsigned long num = bytes[3] & 0xFF;
num |= ((bytes[2] << 8) & 0xFF00);
num |= ((bytes[1] << 16) & 0xFF0000);
num |= ((bytes[0] << 24) & 0xFF000000);
return num;
}
unsigned long CPublicFunction::GenerateCRC32(unsigned char *DataBuf,unsigned long len)
{
byte bSrc[4];
unsigned long crc;
unsigned long i,j;
unsigned long temp;
crc = 0xFFFFFFFF;
for( i=0;i<len;i++)
{
ZeroMemory(bSrc,4);
if (i == len-1)
{
if (DataBuf[i*4] ==0xcd)
{
DataBuf[i*4] =0x00;
}
if (DataBuf[i*4+1] ==0xcd)
{
DataBuf[i*4+1] =0x00;
}
if (DataBuf[i*4+2] ==0xcd)
{
DataBuf[i*4+2] =0x00;
}
if (DataBuf[i*4+3] ==0xcd)
{
DataBuf[i*4+3] =0x00;
}
}
bSrc[0] = DataBuf[i*4];
bSrc[1] = DataBuf[i*4+1];
bSrc[2] = DataBuf[i*4+2];
bSrc[3] = DataBuf[i*4+3];
temp = byte_int(bSrc);
for(j=0;j<32;j++)
{
if( (crc ^ temp) & 0x80000000 )
{
crc = 0x04C11DB7 ^ (crc<<1);
}else
{
crc <<=1;
}
temp<<=1;
}
}
return crc;
}
未完待续。。。。。