| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046 |
- //////////////////////////////////////////////////////////////////////////////
- ////// //////
- ////// 文 件: Global.cpp //////
- ////// 作 者: jesse //////
- ////// 创建时间: 2010-04-15 //////
- ////// 说 明: 定义全局变量,全局函数 //////
- ////// //////
- ////// 修改时间: //////
- ////// 修改说明: //////
- ////// //////
- //////////////////////////////////////////////////////////////////////////////
- #pragma once
- #include "stdafx.h"
- #include "Global.h"
- #include <cmath>
- #include <bitset>
- INT g_nPrecision = 4;
- /*字符大小写互换*/
- void UpperLowerSwap(char *szMsg)
- {
- for (int i=0; i< (int)strlen(szMsg); i++)
- {
- if ((szMsg[i]>='A') && (szMsg[i]<='Z'))
- {
- szMsg[i] = szMsg[i] + 32;
- }
- else if ((szMsg[i]>='a') && (szMsg[i]<='z'))
- {
- szMsg[i] = szMsg[i] - 32;
- }
- else
- {
- szMsg[i] = szMsg[i];
- }
- }
- }
- /*字符全转为大写*/
- void ByteToUpper(char *szMsg)
- {
- for (int i=0; i < static_cast<int>(strlen(szMsg)); i++)
- {
- if ((szMsg[i]>='a') && (szMsg[i]<='z'))
- {
- szMsg[i] = szMsg[i] - 32;
- }
- else
- {
- szMsg[i] = szMsg[i];
- }
- }
- }
- /*字符全转为小写*/
- void ByteToLower(char *szMsg)
- {
- for (int i=0; i < static_cast<int>(strlen(szMsg)); i++)
- {
- if ((szMsg[i]>='A') && (szMsg[i]<='Z'))
- {
- szMsg[i] = szMsg[i] + 32;
- }
- else
- {
- szMsg[i] = szMsg[i];
- }
- }
- }
- /*一串字符的每个字节转换为两个字节*/
- //szMsg, 要转换的消息
- //szConvMsg 转换后的消息
- void ByteToTwoByte( char *szMsg,char *szConvMsg)
- {
- char ch[3];
- for (int i=0; i < static_cast<int>(strlen(szMsg)); i++)
- {
- memset(ch, 0, 3);
- //itoa(int(szMsg[i]), ch,16);
- itoa(static_cast<int>(szMsg[i]), ch, 16);
- szConvMsg[i*2] = ch[0];
- szConvMsg[i*2 + 1] = ch[1];
- }
- }
- /*一串字符的每两个字节转换为一个字节*/
- void TwoByteToByte(
- char *szMsg, /*要转换的消息*/
- char *szConvMsg /*转换后的消息*/
- )
- {
- for (int i=0; i< static_cast<int>(strlen(szMsg)/2); i++)
- {
- szConvMsg[i] = szMsg[i*2] << 4 | szMsg[i*2 + 1];
- }
- }
- /*十六进制Ascii字符转整数*/
- INT AsciiToBYTE(char szMsg)
- {
- int iConvMsg = static_cast<int>(szMsg);
- //iConvMsg = (int)szMsg;
- if ((szMsg >= 'A')&&(szMsg <= 'F'))
- {
- iConvMsg = iConvMsg - 'A' + 10;
- }
- else if ((szMsg >= 'a')&&(szMsg <= 'f'))
- {
- iConvMsg = iConvMsg - 'a' + 10;
- }
- else if ((szMsg >= '0')&&(szMsg <= '9'))
- {
- iConvMsg -= '0';
- }
- return iConvMsg;
- }
- /*获得chksum*/
- void GetChkSum(
- char *szMsg, /*要转换的消息*/
- char *szConvMsg, /*转换后的消息*/
- int len
- )
- {
- unsigned short iMsg;
- iMsg = 0;
- for (int i=1;i<=(len-6);i++)
- {
- iMsg += szMsg[i]; //ASSIC码值相加
- }
- iMsg = iMsg%65536;
- iMsg = ~iMsg + 1;
- itoa(iMsg,szConvMsg,16);
- ByteToUpper(szConvMsg);
- }
- /*电总协议chksum校验*/
- bool ChkSumCheck(
- char *szMsg, /*要转换的消息*/
- int len
- )
- {
- char szConvMsg[5]; /*转换后的消息*/
- char ChkSum[5];
- int j = 0;
- memset(szConvMsg, 0, 5);
- memset(ChkSum, 0, 5);
- for (int i=(len-5); i<(len-1); i++)
- {
- ChkSum[j] = szMsg[i];
- j++;
- }
- GetChkSum(szMsg, szConvMsg, len);
- ByteToUpper(szConvMsg);
- ByteToUpper(ChkSum);
- if (memcmp(szConvMsg, ChkSum, 4) == 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- /*电总协议Length校验*/
- bool CheckLength( char *szMsg )
- {
- //char *szConvMsg;
- WORD len ,index , lenID;
- len = 4;
- index = 9;
- char ch[4];
- for (int i=0;i<len;i++)
- {
- ch[i] = szMsg[index+i];
- }
- lenID = AsciiToBYTE(ch[1]) + AsciiToBYTE(ch[2]) + AsciiToBYTE(ch[3]);
- lenID = lenID%16;
- lenID = ~lenID + 1;
- lenID = lenID & 15;
- if (AsciiToBYTE(ch[0]) == lenID)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- /*字符转ASCII*/
- char ByteToAscii(BYTE btSrc)
- {
- char chDest;
- if( btSrc < 10 )
- {
- chDest = static_cast<char>(btSrc % 10 + '0');
- chDest = lowercase2uppercase(chDest);
- return chDest;
- }
- else
- {
- chDest = ByteToAscii( btSrc / 10 ) + static_cast<char>( btSrc % 10 + '0' );
- chDest = lowercase2uppercase(chDest);
- return chDest;
- }
- }
- char lowercase2uppercase(BYTE btSrc)
- {
- if( btSrc >= 'a' && btSrc <= 'z' )
- {
- return btSrc - 'a' + 'A';
- }
- return btSrc;
- }
- INT DataConversion(char *szType, char *szMsg, char *szConvMsg, int len, int StaBit, int EndBit)
- {
- if ( strcmp(szType, "FLOAT") == 0)
- {
- TypeIsFLOAT(szMsg, szConvMsg);
- }
- else if( strcmp(szType, "WORD") == 0)
- {
- TypeIsWORD(szMsg, szConvMsg);
- }
- else if( strcmp(szType, "SHORT") == 0)
- {
- TypeIsSHORT(szMsg, szConvMsg);
- }
- else if ( strcmp(szType, "BYTE") == 0 )
- {
- TypeIsBYTE(reinterpret_cast<BYTE *>(szMsg), reinterpret_cast<BYTE *>(szConvMsg), len);
- }
- else if ( strcmp(szType, "CHAR") == 0 )
- {
- TypeIsCHAR(szMsg, szConvMsg, len);
- }
- else if( strcmp(szType, "BIT") == 0)
- {
- TypeIsBIT(szMsg, szConvMsg, len, StaBit, EndBit);
- }
- else if (strcmp(szType, "UINT") == 0)
- {
- TypeIsUINT(szMsg, szConvMsg, len);
- }
- else if (strcmp(szType, "ONEBYTE") == 0)
- {
- TypeIsONEBYTE(szMsg, szConvMsg, len);
- }
- else if ( strcmp(szType,"DL") == 0)
- {
- _BYTE_1_Decimal(szConvMsg,(BYTE*)szMsg);
- }
- else if ( strcmp(szType,"WBIT") == 0)
- {
- int nSour = szMsg[0]; //ASCII_to_Byte(szMsg[0]) *16 + ASCII_to_Byte(szMsg[1]);
- GetWORDBit(szConvMsg,nSour,StaBit,EndBit);
- }
- return 0;
- }
- //将32 33 37 2E 30 转换为237.0
- INT TypeIsUINT(char *szMsg, char *szConvMsg, int len)
- {
- char strBuf[10] = {0};
- //memcpy(szConvMsg, szMsg, len);
- sprintf(szConvMsg,"%lf",atof(szMsg));
- return 0;
- }
- //一个字节 0x31转换为1
- INT TypeIsONEBYTE(char *szMsg, char *szConvMsg, int len)
- {
- char strBuf[5] = {0};
- szConvMsg[0] = szMsg[0];
- return 0;
- }
- INT TypeIsFLOAT(char *szMsg, char *szConvMsg)
- {
- union
- {
- float fNum ;
- BYTE ch[4];
- }uBtoF;
- BYTE ch[8] = {0};
- memcpy(ch, szMsg, 8);
- //LOG4C((LOG_NOTICE,"szMsg = %s ; szConvMsg = %s", szMsg, szConvMsg));
- uBtoF.ch[0] = (AsciiToBYTE(szMsg[0])<<4) | AsciiToBYTE(szMsg[1]);
- uBtoF.ch[1] = (AsciiToBYTE(szMsg[2])<<4) | AsciiToBYTE(szMsg[3]);
- uBtoF.ch[2] = (AsciiToBYTE(szMsg[4])<<4) | AsciiToBYTE(szMsg[5]);
- uBtoF.ch[3] = (AsciiToBYTE(szMsg[6])<<4) | AsciiToBYTE(szMsg[7]);
- sprintf(szConvMsg,"%f",uBtoF.fNum);
- //LOG4C((LOG_NOTICE,"ch = %s ; szConvMsg = %s", ch, szConvMsg));
- return 0;
- }
- INT TypeIsSHORT(char *szMsg, char *szConvMsg)
- {
- WORD hW;
- BYTE a[2] = {0};
- BYTE ch[4] = {0};
- memcpy(ch, szMsg, 4);
- a[0] = AsciiToBYTE(ch[0]) << 4 | AsciiToBYTE(ch[1]);
- a[1] = AsciiToBYTE(ch[2]) << 4 | AsciiToBYTE(ch[3]);
- hW = (a[0] & 0xFFFF) << 8 | ( a[1] & 0xFFFF );
- //hW = atoi(a);
- sprintf(szConvMsg,"%d",hW);
- return 0;
- }
- INT TypeIsWORD(char *szMsg, char *szConvMsg)
- {
- WORD hW;
- BYTE a[2] = {0};
- int i;
- BYTE ch[4] = {0};
- for (i=0;i<4;i++)
- {
- ch[i] = szMsg[i];
- }
- a[0] = AsciiToBYTE(ch[0])*16 + AsciiToBYTE(ch[1]);
- a[1] = AsciiToBYTE(ch[2])*16 + AsciiToBYTE(ch[3]);
- hW = (a[0] & 0xFFFF) << 8 | ( a[1] & 0xFFFF );
- //hW = atoi(a);
- sprintf(szConvMsg,"%d",hW);
- //LOG4C((LOG_NOTICE,"szMsg = %s ; szConvMsg = %s", szMsg, szConvMsg))
- return 0;
- }
- // 市委未用到,回去再改
- INT TypeIsDWORD(char *szMsg, char *szConvMsg)
- {
- char nbuf[VAR_MSG] = {0};
- char nchar;
- char nchar1;
- char nchar2;
- char nchar3;
- for (int i = 0; i<(int)strlen(szMsg); i++)
- {
- nbuf[i] = static_cast<char>(AsciiToBYTE(szMsg[i]));
- }
- nchar = nbuf[0]<<4 | nbuf[1];
- nchar1 = nbuf[2]<<4 | nbuf[3];
- nchar2 = nbuf[4]<<4 | nbuf[5];
- nchar3 = nbuf[6]<<4 | nbuf[7];
- if ((nchar == 0) && (nchar1 == 0) && (nchar2 == 0) && (nchar3 == 0))
- {
- sprintf(szConvMsg, "0");
- }
- else
- {
- sprintf(szConvMsg, "%c%c%c%c",nchar, nchar1, nchar2, nchar3);
- }
- return 0;
- }
- INT TypeIsBYTE(BYTE *szMsg, BYTE *szConvMsg, int len)
- {
- //LOG4C((LOG_NOTICE, "TypeIsBYTE"));
- //LOG4C_HEX_DUMP((LOG_INFO, (char *)szMsg, 5));
- BYTE btChar;
- char ch[3] = {0};
- for (int i=0; i<len/2; i++)
- {
- for (int j=0; j<2; j++)
- {
- ch[j] = szMsg[j + i*2];
- }
- }
- btChar = AsciiToBYTE(ch[0]) << 4 | AsciiToBYTE(ch[1]);
- sprintf(reinterpret_cast<char*>(szConvMsg),"%d",btChar);
- //szConvMsg[i] = AsciiToBYTE(ch[0]) << 4 | AsciiToBYTE(ch[1]);
- return 0;
- }
- INT TypeIsCHAR(char *szMsg, char *szConvMsg, int len)
- {
- BYTE ch[3];
- for (int i=0; i<len/2; i++)
- {
- memset(ch, 0, 2);
- for (int j=0; j<2; j++)
- {
- ch[j] = szMsg[j + i*2];
- }
- szConvMsg[i] += AsciiToBYTE(ch[0]) << 4 | AsciiToBYTE(ch[1]);
- }
- return 0;
- }
- INT TypeIsBIT(char *szMsg, char *szConvMsg, int len, int StaBit, int EndBit)
- {
- char bytValue[2];
- int ByteBit = 0;
- memset(bytValue, 0, 2);
- TypeIsCHAR(szMsg, bytValue, len);
- bytValue[0] = AsciiToBYTE(bytValue[0]);
- //int Len = sizeof(bytValue);
- if (StaBit == EndBit)
- {
- switch(StaBit)
- {
- case 0:
- ByteBit = bytValue[0] & 0x01;
- break;
- case 1:
- ByteBit = (bytValue[0] & 0x02) / 0x02 ;
- break;
- case 2:
- ByteBit = (bytValue[0] & 0x04) / 0x04 ;
- break;
- case 3:
- ByteBit = (bytValue[0] & 0x08) / 0x08 ;
- break;
- case 4:
- ByteBit = (bytValue[0] & 0x10) / 0x10 ;
- break;
- case 5:
- ByteBit = (bytValue[0] & 0x20) / 0x20 ;
- break;
- case 6:
- ByteBit = (bytValue[0] & 0x40) / 0x40 ;
- break;
- case 7:
- ByteBit = (bytValue[0] & 0x80) / 0x80;
- break;
- }
- _snprintf(szConvMsg,sizeof(szConvMsg),"%d",ByteBit);
- }
- else
- {
- int tempbit = StaBit;
- int TiLen = EndBit - StaBit;
- for (int i = 0; i < TiLen;i++)
- {
- tempbit = tempbit + i;
- switch(tempbit)
- {
- case 0:
- ByteBit = bytValue[0] & 0x01;
- break;
- case 1:
- ByteBit = (bytValue[0] & 0x02) / 0x02 ;
- break;
- case 2:
- ByteBit = (bytValue[0] & 0x04) / 0x04 ;
- break;
- case 3:
- ByteBit = (bytValue[0] & 0x08) / 0x08 ;
- break;
- case 4:
- ByteBit = (bytValue[0] & 0x10) / 0x10 ;
- break;
- case 5:
- ByteBit = (bytValue[0] & 0x20) / 0x20 ;
- break;
- case 6:
- ByteBit = (bytValue[0] & 0x40) / 0x40 ;
- break;
- case 7:
- ByteBit = (bytValue[0] & 0x80) / 0x80;
- break;
- }
- _snprintf(szConvMsg + i,sizeof(szConvMsg),"%f",ByteBit);
- }
- }
- return 0;
- }
- int DigitToBinary(WORD wdSource, char* pDes, int iBit)
- {
- char pTmpBuf[16] = {0};
- char chBuffer[16] = {0};
- //wdSource =htonl(wdSource);
- itoa(wdSource, pTmpBuf, 2);
- int iLen = static_cast<int>(strlen(pTmpBuf)) - 1;
- char chValue[16] = {0};
- strcpy(chValue, pTmpBuf);
- for (int i =0; i<=iLen; i++)
- {
- pTmpBuf[i] = chValue[iLen - i];
- }
- for (int k = 0; k<iBit; k++)
- {
- if ( 0x00 == pTmpBuf[iBit - k - 1])
- chBuffer[k] = 0x30;
- else
- chBuffer[k] = pTmpBuf[iBit - k - 1];
- }
- memcpy(pDes, chBuffer, iBit);
- return 0;
- }
- void strReverse( char *str )
- {
- int l = (int)strlen(str);
- for( int i = 0; i < l; i++ )
- {
- for(int i = 0; i < l; i++)
- {
- if( str[i] >= 'A' && str[i] <= 'Z' )
- {
- str[i] += 32;
- }
- else if(str[i] >= 'a' && str[i] <= 'z')
- {
- str[i] -= 32;
- }
- }
- }
- }
- //由数据精度获得浮点数的字符串值
- CString GetDoubleText(double d)
- {
- CString strRtn;
- CString strFmt;
- if(g_nPrecision == 0)
- strFmt = "%g";
- else
- {
- long lV = static_cast<long>(fabs(d));
- if(lV >= 1)
- {
- int nIntBit = 0;
- while(lV >= 1)
- {
- lV = lV/10;
- nIntBit++;
- }
- if(nIntBit < g_nPrecision)
- strFmt.Format("%c%d.%df", '%', nIntBit, (g_nPrecision-nIntBit));
- else
- strFmt.Format("%c%d.0f", '%', nIntBit);
- }
- else
- {
- double dTemp = fabs(d);
- if(dTemp > 0.00000000001)
- {
- int nDot0Bit = 0;
- while(dTemp < 0.1)
- {
- dTemp = dTemp*10;
- nDot0Bit++;
- }
- strFmt.Format("%c1.%df", '%', nDot0Bit + g_nPrecision);
- }
- else
- {
- strFmt = "%g";
- }
- }
- }
- strRtn.Format(strFmt, d);
- return strRtn;
- }
- char Hex16(char WillChangeNum[]) //该函数把四位二进制转换成十六进制数
- {
- int i;
- i = (WillChangeNum[3]) + (WillChangeNum[2] * 10) +
- (WillChangeNum[1] * 100) + (WillChangeNum[0] * 1000);
- switch(i)
- {
- case 0:
- return 0;
- case 1:
- return 1;
- case 10:
- return 2;
- case 11:
- return 3;
- case 100:
- return 4;
- case 101:
- return 5;
- case 110:
- return 6;
- case 111:
- return 7;
- case 1000:
- return 8;
- case 1001:
- return 9;
- case 1010:
- return 0x0A;
- case 1011:
- return 0x0B;
- case 1100:
- return 0x0C;
- case 1101:
- return 0x0D;
- case 1110:
- return 0x0E;
- case 1111:
- return 0x0F;
- }
- return -1;
- }
- #define CPH 0
- #define CPL 1
- static unsigned char tbcrch[] = {
- 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64,
- 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65,
- 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65,
- 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64,
- 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65,
- 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64,
- 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64,
- 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65,
- 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65,
- 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64,
- 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64,
- 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65,
- 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64,
- 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65,
- 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65,
- 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64,};
- static unsigned char tbcrcl[] = {
- 0,192,193,1,195,3,2,194,198,6,7,199,5,197,196,4,
- 204,12,13,205,15,207,206,14,10,202,203,11,201,9,8,200,
- 216,24,25,217,27,219,218,26,30,222,223,31,221,29,28,220,
- 20,212,213,21,215,23,22,214,210,18,19,211,17,209,208,16,
- 240,48,49,241,51,243,242,50,54,246,247,55,245,53,52,244,
- 60,252,253,61,255,63,62,254,250,58,59,251,57,249,248,56,
- 40,232,233,41,235,43,42,234,238,46,47,239,45,237,236,44,
- 228,36,37,229,39,231,230,38,34,226,227,35,225,33,32,224,
- 160,96,97,161,99,163,162,98,102,166,167,103,165,101,100,164,
- 108,172,173,109,175,111,110,174,170,106,107,171,105,169,168,104,
- 120,184,185,121,187,123,122,186,190,126,127,191,125,189,188,124,
- 180,116,117,181,119,183,182,118,114,178,179,115,177,113,112,176,
- 80,144,145,81,147,83,82,146,150,86,87,151,85,149,148,84,
- 156,92,93,157,95,159,158,94,90,154,155,91,153,89,88,152,
- 136,72,73,137,75,139,138,74,78,142,143,79,141,77,76,140,
- 68,132,133,69,135,71,70,134,130,66,67,131,65,129,128,64,};
- /********************************************************************
- * NAME : TwoHexCharToChar
- * FUNCTION :
- * PROCESS :
- * INPUT :
- * OUTPUT :
- * UPDATE :
- * RETURN :
- * :
- * PROGRAMMED :
- * DATE(ORG) :
- * CALL :
- * SYSTEM :
- ********************************************************************/
- char TwoHexCharToChar(char ch1,char ch2)
- {
- char Numb1;
- char Numb2;
- if (ch1 >= 'A')
- Numb1 = (toupper(ch1)-'0'-7)*16;
- else
- Numb1 = (ch1 - '0')*16;
- if (ch2 >= 'A')
- Numb2 = (toupper(ch2) - '0' - 7);
- else
- Numb2 = (ch2 - '0');
- return (Numb1 + Numb2);
- }
- /********************************************************************
- * NAME : Str2HexStr
- * FUNCTION :
- * PROCESS :
- * INPUT :
- * OUTPUT :
- * UPDATE :
- * RETURN :
- * :
- * PROGRAMMED :
- * DATE(ORG) :
- * CALL :
- * SYSTEM :
- ********************************************************************/
- void Str2HexStr(char *szHexString,char *szString,int *iHexStringLen)
- {
- int iLoop;
- int iCount;
-
- iLoop = 0;
- iCount = 0;
- szHexString[0] = '\0';
-
- while(1)
- {
- if (szString[iLoop] == '\0' || szString[iLoop + 1] == '\0')
- {
- break;
- }
-
- szHexString[iCount] = static_cast<char>(TwoHexCharToChar(szString[iLoop],szString[iLoop + 1]));
- iLoop ++;
- iLoop ++;
- iCount ++;
- }
-
- szHexString[iCount] = '\0';
-
- *iHexStringLen = iCount;
- }
- /*
- Convert a binary data buffer to a hex string
- str: output (a string like “20ef9a“)
- bin: input (a data buffer)
- binlen: input (the length of the data buffer)
- */
- void bin2str(char *str, const unsigned char *bin, int binlen)
- {
- int i;
- for(i=0; i<binlen; i++)
- {
- str[2*i] = ((bin[i] & 0xF0) >> 4);
- str[2*i] = (str[2*i] > 9) ? (str[2*i] + 'A' - 10) : (str[2*i] + '0');
- str[2*i+1] = (bin[i] & 0x0F);
- str[2*i+1] = (str[2*i+1] > 9) ? (str[2*i+1] + 'A' - 10) : (str[2*i+1] + '0');
- }
- str[2*i] = '\0';
- }
- // 将10进制转换为2进制字符串;
- char *dectobin(int dec,int len)
- {
- int i=0;
- static char buf[256];
- char szbuf1[4] = {0};
- char szbuf2[4] = {0};
- char szbuf3[4] = {0};
- #if 0
- for (i = 0; i< 4;i++)
- {
- szbuf1[i] = dec%2+48;
- dec = dec/2;
- }
- for (i = 0; i< 4;i++)
- {
- szbuf2[i] = dec%2+48;
- dec = dec/2;
- }
- for (i = 0; i< 4;i++)
- {
- szbuf3[i] = dec%2+48;
- dec = dec/2;
- }
- #endif
- #if 1
- memset(buf,0,256);
- memset(buf,0,len);
- while(dec != 0)
- {
- buf[i++]=dec%2+48;
- dec=dec/2;
- }
- #endif
- return strrev(buf);
- }
- // 算法:calc length by lenid;
- void ReadDZLegthbyLenid(BYTE LenID[2],BYTE Length[4])
- {
- int ndec = atoi((char*)LenID);
- Length = reinterpret_cast<BYTE*>(dectobin(ndec,12));
- return;
- }
- // 将16进制字符串按顺序转为10进制字符串;
- // 高在前,低在后;
- int HexStr2Dec(char *Source)
- {
- int iLen,temp = 0,n;
- iLen = (int)strlen(Source);//十六进制是按字符串传进来的,所以要获得他的长度;
- for( int i = 0; i < iLen; i++)
- {
- if(Source[i] >= 'A' && Source[i] <= 'F') //十六进制还要判断他是不是在A-F或者a-f之间a=10;
- n = Source[i] - 'A' + 10;
- else if(Source[i] >= 'a' && Source[i] <= 'f')
- n = Source[i] - 'a' + 10;
- else n = Source[i] - '0';
- temp = temp*16 + n;
- }
- return temp;
- }
- // 取字节的位;
- void ONEBYTE_BIT(char *szValue, const int &iSour, const int &iSBit, const int &iEBit)
- {
- if ( iSBit > iEBit)
- return;
- bitset<8> bitSor(iSour);
- char szBinary[9] = {0};
- for ( int i = 0; i < 8; i++)
- szBinary[i] = bitSor[i];
- if ( iSBit == iEBit)
- sprintf(szValue,"%d",(int)bitSor[iSBit]);
- else
- {
- // biset是从位0开始的;
- for (int i = 0; i < 8; i++)
- {
- if ( i < iSBit || i > iEBit)
- bitSor.reset(i);
- }
- itoa(bitSor.to_ulong(),szValue,10);
- }
- }
- void GetBYTEBit(char *szValue, const int &iSour, const int &iSBit, const int &iEBit)
- {
- if ( iSBit > iEBit)
- return;
- bitset<8> bitSor(iSour);
- char szBinary[9] = {0};
- for ( int i = 0; i < 8; i++)
- szBinary[i] = bitSor[i];
- if ( iSBit == iEBit)
- sprintf(szValue,"%d",(int)bitSor[iSBit]);
- else
- {
- // 以下无效;
- for (int i = 0; i < 8; i++)
- {
- if ( i < iSBit || i > iEBit)
- bitSor.reset(i);
- }
- itoa(bitSor.to_ulong(),szValue,10);
- }
- }
- // 取字节的位;
- void GetWORDBit(char *szValue, const int &iSour, const int &iSBit, const int &iEBit)
- {
- if ( iSBit > iEBit)
- return;
- bitset<16> bitSor(iSour);
- char szBinary[17] = {0};
- for ( int i = 0; i < 16; i++)
- szBinary[i] = bitSor[i];
- if ( iSBit == iEBit)
- sprintf(szValue,"%d",(int)bitSor[iSBit]);
- }
- // 取字节的位;
- void ONEBYTE_BIT2(char *szValue, const int &iSour, const int &iSBit, const int &iEBit)
- {
- if ( iSBit > iEBit)
- return;
- //int iSour = szCopy[0];
- // 位顺序: 高->低 (7~0);
- bitset<8> bitSor(iSour);
- char szBinary[9] = {0};
- for ( int i = 0,j = 7; i < 8; i++,j--)
- szBinary[j] = bitSor[i] + 48;
- char szCut[3] = {0};
- for ( int i = iSBit,j = 0; i <= iEBit; i++,j++)
- szCut[j] = szBinary[i];
- if ( strcmp(szCut,"00") == 0)
- szValue[0] = '0';
- else if ( strcmp(szCut,"01") == 0)
- szValue[0] = '1';
- else if ( strcmp(szCut,"10") == 0)
- szValue[0] = '2';
- else if ( strcmp(szCut,"11") == 0)
- szValue[0] = '3';
- }
- int GetIniInfo(char *szPath,char *szIniName,char *szCmd,char *IniSendCMD,int &IniSendlen,char *szDataType,int &nIndex,int &nLen, int &iSBit, int &iEBit)
- {
- CHAR szFile[MAX_PATH + 1] = "";
- wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
- IniSendlen = GetPrivateProfileString(szCmd, "SendCmd", "", IniSendCMD, 10, szFile); // 返回的字符串是以\0结束的;
- GetPrivateProfileString(szCmd, "type", "", szDataType, 10, szFile);
- szDataType[strlen(szDataType)] = '\0';
- nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
- nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
- iSBit = GetPrivateProfileInt(szCmd, "iSBit", 0, szFile);//从配置文件中取值
- iEBit = GetPrivateProfileInt(szCmd, "iEBit", 0, szFile);
- return 0;
- }
- UINT GetModbus16CRC(BYTE *pBuf, int len)
- {
- unsigned int Genpoly=0xA001;
- unsigned int CRC=0xFFFF;
- unsigned int index;
- while(len--)
- {
- CRC=CRC^(unsigned int)*pBuf++;
- for(index=0;index<8;index++)
- {
- if((CRC & 0x0001)==1) CRC=(CRC>>1)^Genpoly;
- else (CRC=CRC>>1);
- }
- }
- return(CRC);
- }
- INT ASCII_to_Byte(char c)
- {
- int iConvt = static_cast<int>(c);
- if ((c >= 'A')&&(c <= 'F'))
- iConvt = iConvt - 'A' + 10;
- else if ((c >= 'a')&&(c <= 'f'))
- iConvt = iConvt - 'a' + 10;
- else if ((c >= '0')&&(c <= '9'))
- iConvt -= '0';
- return iConvt;
- }
- int digit_to_hex(BYTE by)
- {
- if ( by >= 0 && by <= 9 )
- return by;
- char szRet[3] = {0};
- itoa(by,szRet,10);
- return TwoHexCharToChar(szRet[0],szRet[1]);
- }
- WORD ReturnASCIIWORD(BYTE *bySource)
- {
- WORD wValue = 0;
- wValue = bySource[0] << 8 | bySource[1];
- return wValue;
- }
- WORD ReturnNASCIIWORD(BYTE *bySource)
- {
- WORD wValue = 0;
- wValue = bySource[1] << 8 | bySource[0];
- return wValue;
- }
- void _BYTE_2_Decimal(char *szValue, BYTE *szSource)
- {
- // 高在前,低在后;
- WORD wValue = ReturnASCIIWORD(szSource);
- sprintf(szValue,"%d",wValue);
- }
- void _BYTE_1_Decimal(char *szValue, BYTE *szSource)
- {
- // 高在前,低在后;
- sprintf(szValue,"%d",szSource[0]);
- }
|