📄 berdecoder.cpp
字号:
HexArray[r-1]=HexArray[r-1]+0x1; HexArray[r]=0; /////////////////////////////////////////////////////////////////////////// int w=0; for(int k=0;k<r;k++) { DecArray[w++]=HexArray[k]>>4; DecArray[w++]=HexArray[k]&0xf; } ////////////// /// /// //////////////////////////////////////////////////// int dec3bit[2]={0,0}; for(k=0;k<100;k++) ResultArray[k]=0; for(k=0;k<w;k++) { for(int m=99;m>0;m--) TempResult[m-1]=ResultArray[m]; TempResult[99]=0; // dec3bit[0]=DecArray[k]/10; dec3bit[1]=DecArray[k]%10; // int add=0; int t1=0,t2=0,t3=0; for(int n=99;n>0;n--) { t1=(ResultArray[n]*6+add)%10; add=(ResultArray[n]*6+add)/10; ResultArray[n]=t1; } add=0; for( n=99;n>0;n--) { t2=((ResultArray[n]+add+TempResult[n]))%10; add=((ResultArray[n]+add+TempResult[n]))/10; ResultArray[n]=t2; } add=0; t1=((ResultArray[99]+add+dec3bit[1]))%10; add=((ResultArray[99]+add+dec3bit[1]))/10; ResultArray[99]=t1; t2=((ResultArray[98]+add+dec3bit[0]))%10; add=((ResultArray[98]+add+dec3bit[0]))/10; ResultArray[98]=t2; for( n=97;n>0;n--) { t1=(ResultArray[n]+add)%10; add=(ResultArray[n]+add)/10; ResultArray[n]=t1; } } int tag0=1; int pos=1; char tempString[100]; //tempString=new char[100-pos+1]; for(int n=0;n<100;n++) { if((ResultArray[n]!=0)&&(tag0==1)) tag0=0; if (tag0==0){tempString[pos++]=ResultArray[n]+48;} } tempString[0]='-'; tempString[pos]=0; out=tempString; //delete []tempString; /////////////////////////////////////////////////////////////////////// } //////////////////////////////////////////////////////////////////////// //标志位为 '0' 直接求和 else { // //printf(" <%x> \n",HexArray[r-1]); for(int i=0;i<nLength;i++) HexArray[r++]=(unsigned short)temp[i]; HexArray[r]=0; /////////////////////////////////////////////////////////////////////////// int w=0; for(int k=0;k<r;k++) { DecArray[w++]=HexArray[k]>>4; DecArray[w++]=HexArray[k]&0xf; } ////////////// /// /// //////////////////////////////////////////////////// int dec3bit[2]={0,0}; for(k=0;k<100;k++) ResultArray[k]=0; for(k=0;k<w;k++) { for(int m=99;m>0;m--) TempResult[m-1]=ResultArray[m]; TempResult[99]=0; // dec3bit[0]=DecArray[k]/10; dec3bit[1]=DecArray[k]%10; // int add=0; int t1=0,t2=0,t3=0; for(int n=99;n>0;n--) { t1=(ResultArray[n]*6+add)%10; add=(ResultArray[n]*6+add)/10; ResultArray[n]=t1; } add=0; for( n=99;n>0;n--) { t2=((ResultArray[n]+add+TempResult[n]))%10; add=((ResultArray[n]+add+TempResult[n]))/10; ResultArray[n]=t2; } add=0; t1=((ResultArray[99]+add+dec3bit[1]))%10; add=((ResultArray[99]+add+dec3bit[1]))/10; ResultArray[99]=t1; t2=((ResultArray[98]+add+dec3bit[0]))%10; add=((ResultArray[98]+add+dec3bit[0]))/10; ResultArray[98]=t2; for( n=97;n>0;n--) { t1=(ResultArray[n]+add)%10; add=(ResultArray[n]+add)/10; ResultArray[n]=t1; } } int tag0=1; int pos=0; char tempString[100]; //tempString=new char[100-pos+1]; for(int n=0;n<100;n++) { if((ResultArray[n]!=0)&&(tag0==1)) tag0=0; if (tag0==0){tempString[pos++]=ResultArray[n]+48;} } //tempString[0]='-'; tempString[pos]=0; out=tempString; //delete []tempString; } //free(temp); return 1; } // 1.分解1、2字节 // 2.按两部分用integer解码指数和尾数。 // 3.符号×尾数×(进制)**指数 -》最终解 bool CBERDecoder::decodeReal(const char* szInputStream, int nLength, double& out) { int sign=0; int expLength,massLength; int expValue,massValue; char *exp,*massita; unsigned char *temp; temp=(unsigned char *)calloc(sizeof(char),nLength); memcpy(temp,szInputStream,nLength); printf("\n%s\nhead :%c length :%c ",temp,temp[0],temp[1]); if((temp[0]>>7)==1) { cout<<"\nErr: this function not supported\n"; return 0; } if (((temp[0]>>6)&0x1)==1) sign=-1; else sign=1; expLength=(int)temp[1]; //printf("\ntemp[1]:%d\n",temp[1]); massLength=nLength-expLength-2;//-2 exp =new char[expLength+1]; for(int i=0;i<expLength;i++) { exp[i]=temp[i+2]; } exp[expLength+2]=0; massita=new char[massLength+1]; for(int k=0;k<massLength;k++) { massita[k]=temp[expLength+k+2]; } massita[massLength+2+expLength]=0; // cout<<"\nszInputStream :"<<szInputStream<<"\nnLength:"<<nLength; // cout<<"\nexp:"<<exp<<"\nexpLength:"<<expLength<<endl; // cout<<"\nmassita:"<<massita<<"\nmassLength:"<<massLength<<endl; decodeInteger(exp,expLength,expValue); cout<<"\nExponext :"<<expValue; decodeInteger(massita,massLength,massValue); cout<<"\nMassita:"<<massValue; out=(double)sign*massValue*pow(10.,expValue); delete []exp; delete []massita; free(temp); return 1; } /* //function //universial 2 //Octet String 8bits flow bool CBERDecoder::decodeOctetString(const char* szInputStream, int nLength, char* out,int& len) { //szInputStream //out=new char[len]; // strcpy(out, szInputStream); return 1; }; */ bool CBERDecoder::decodeNull(const char* szInputStream, int nLength) { //m_szInputStream return 0; }; /* bool CBERDecoder::decodeEnumerated (const char* szInputStream, int nLength, int& out) { decodeInteger(szInputStream, nLength, out); return 1; }; */ bool CBERDecoder::decodeUTF8String(const char* szInputStream, int nLength, String& out) { for(int i=2;i<nLength;i++) { out+=(char)(szInputStream[i]&0x7f); } return 1; }; bool CBERDecoder::decodeNumericString(const char* szInputStream, int nLength, String& out) { char temp; //int t; for(int i=2;i<nLength;i++) { //t=atoi(szInputStream[i]);tinue if (szInputStream[i]<'0'||szInputStream[i]>'9') continue; sprintf(&temp,"%c",szInputStream[i]); out+=temp; // //t=(int)szInputStream[i]>>4; //sprintf(&temp,"%d",t); //out+=temp; //t=(int)szInputStream[i]&0xf; //sprintf(&temp,"%d",t); //out+=temp; } return 1; }; bool CBERDecoder::decodeIA5String(const char* szInputStream, int nLength, String& out) { //BY ZHST char* pOut = (char*)malloc(nLength+1); if(pOut != NULL) { memcpy(pOut,szInputStream, nLength); pOut[nLength] = 0; out=pOut; free(pOut); return true; } return false; }; bool CBERDecoder::decodePrintableString(const char* szInputStream, int nLength, String& out) { out+=szInputStream; return 1; }; bool CBERDecoder::decodeTeletexString(const char* szInputStream, int nLength, String& out) { out+=szInputStream; return 1; }; bool CBERDecoder::decodeVisibleString(const char* szInputStream, int nLength, String& out) { out+=szInputStream; return 1; }; bool CBERDecoder::decodeString(const char* szInputStream, int nLength, String& out) { //out+=szInputStream; char* a = (char*)malloc(nLength+1); if(a != NULL) { memcpy(a, szInputStream, nLength); a[nLength] = 0; out = a; free(a); return true; } return false; }; bool CBERDecoder::decodeUTCTime(const char* szInputStream, int nLength, String& out) { char finaltempstring[15];//*timestring,,hour[2] int h,m; int signpos,timevalue[9],p=0; time_t tvalue; struct tm *ttemp; // step1 : 初始化结构 ttemp ,对结构成员赋值,对有些11 13秒值赋零 // step2 : 取出time_t值,对相应落后提前值进行修正 // // step3 : 根据time_t 取出时间值,赋值 //去掉头字节 从第三位开始 i=2 time(&tvalue); ttemp=localtime(&tvalue); // for(int i=2;i<nLength;i++) for(int i=0;i<nLength;i++)//by zhst { if ((szInputStream[i]=='+')||(szInputStream[i]=='-')) { signpos=i; p++; continue; } h=int(szInputStream[i]>>4); m=int(szInputStream[i]&0xf); timevalue[p]=h*10+m; p++; } ttemp->tm_year=timevalue[0]+ 100; ttemp->tm_mon=timevalue[1]; ttemp->tm_mday=timevalue[2]; ttemp->tm_hour=timevalue[3]; ttemp->tm_min=timevalue[4]; ttemp->tm_sec=timevalue[5]; if((timevalue[7]!=0)||(timevalue[8]!=0)) { if (szInputStream[8]=='-') { ttemp->tm_hour+=timevalue[7]; ttemp->tm_min+=timevalue[8]; } else { ttemp->tm_hour-=timevalue[7]; ttemp->tm_min-=timevalue[8]; } tvalue=mkgmtime(ttemp); ttemp=gmtime(&tvalue); } sprintf(finaltempstring,"%04d%02d%02d%02d%02d%02d",\ ttemp->tm_year+1900,ttemp->tm_mon,ttemp->tm_mday,\ ttemp->tm_hour,ttemp->tm_min,ttemp->tm_sec); out=finaltempstring; return 1; } bool CBERDecoder::decodeGeneralizedTime(const char* szInputStream, int nLength, String& out) { char finaltempstring[15];//*timestring,,hour[2] int h,m; int signpos,timevalue[10],p=0; time_t tvalue; struct tm *ttemp; // step1 : 初始化结构 ttemp ,对结构成员赋值,对有些11 13秒值赋零 // step2 : 取出time_t值,对相应落后提前值进行修正 // // step3 : 根据time_t 取出时间值,赋值 //去掉头字节 从第三位开始 i=2 time(&tvalue); ttemp=localtime(&tvalue); for(int i=2;i<nLength;i++) { if ((szInputStream[i]=='+')||(szInputStream[i]=='-')) { signpos=i; p++; continue; } h=int(szInputStream[i]>>4);m=int(szInputStream[i]&0xf); timevalue[p]=h*10+m; // printf("\n timevalue [%d] %d",p,timevalue[p]); p++; } ttemp->tm_year=timevalue[0]*100+timevalue[1]-1900; ttemp->tm_mon=timevalue[2]; ttemp->tm_mday=timevalue[3]; ttemp->tm_hour=timevalue[4]; ttemp->tm_min=timevalue[5]; ttemp->tm_sec=timevalue[6]; // printf("\nget is:\n%04d%02d%02d%02d%02d%02d",\ //ttemp->tm_year+1900,ttemp->tm_mon,ttemp->tm_mday,\ //ttemp->tm_hour,ttemp->tm_min,ttemp->tm_sec); if((timevalue[8]!=0)||(timevalue[9]!=0)) { if (szInputStream[signpos]=='-') { ttemp->tm_hour+=timevalue[8]; ttemp->tm_min+=timevalue[9]; } else { ttemp->tm_hour-=timevalue[8]; ttemp->tm_min-=timevalue[9]; } tvalue=mkgmtime(ttemp); ttemp=gmtime(&tvalue); } sprintf(finaltempstring,"%04d%02d%02d%02d%02d%02d",\ ttemp->tm_year+1900,ttemp->tm_mon,ttemp->tm_mday,\ ttemp->tm_hour,ttemp->tm_min,ttemp->tm_sec); out=finaltempstring; return 1; }; time_t CBERDecoder::mkgmtime(struct tm *t) { register short month, year; register time_t result; static int m_to_d[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; month = t->tm_mon; year = t->tm_year + month / 12 + 1900; month %= 12; if (month < 0) { year -= 1; month += 12; } result = (year - 1970) * 365 + (year - 1969) / 4 + m_to_d[month]; result = (year - 1970) * 365 + m_to_d[month]; if (month <= 1) year -= 1; result += (year - 1968) / 4; result -= (year - 1900) / 100; result += (year - 1600) / 400; result += t->tm_mday; result -= 1; result *= 24; result += t->tm_hour; result *= 60; result += t->tm_min; result *= 60; result += t->tm_sec; return(result); }; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -