📄 sharefunc.cpp
字号:
/********************************************************************** FileName : sharefunc.cpp Description : 共享函数集 Version : 1.0 Date : 2003年6月16日 Author : 刘荣辉 Other : ***********************************************************************/#include "../include/sharefunc.h"//================================================================char * CFunc::lrhitoa(int a,char *buf) //整数->字符串转化函数{ int i,n,b; char tmp[15]; for(n=0;;n++) { if(a>=10) b=a%10; else if(a<1) break; else if(a<10) b=a; tmp[n]=48+b; a=(int)(a/10); } for(i=0;i<n;i++) buf[i]=tmp[n-i-1]; buf[i]='\0'; return buf; }//================================================================//获取时间,如"0618172219"(月日时分秒各两位)char * CFunc::setTimeStamp(char *szBuf){ time_t tt; struct tm *now; time(&tt); now = localtime( &tt); sprintf( szBuf, "%02d%02d%02d%02d%02d", now->tm_mon + 1, now->tm_mday, now->tm_hour + 2, now->tm_min, now->tm_sec); return szBuf;}//================================================================//获取时间,如"20030618172219"(年月日时分秒,加上‘\0’共15个字符)char* CFunc::getTime(char* buf){ time_t tt; struct tm *now; time(&tt); now = localtime( &tt); sprintf( buf, "%d%02d%02d%02d%02d%02d", now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); return buf;}//================================================================//获取时间,如"20030618172219"(年月日时分秒,加上‘\0’共15个字符)char* CFunc::getTime2(char* buf){ time_t tt; struct tm *now; time(&tt); now = localtime( &tt); sprintf( buf, "%2d月%2d日%02d:%02d", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min); return buf;}//================================================================//将time_t类型转化成时间字符串(如"20030618172219")char * CFunc::TimeToStr(time_t tt, char *timebuf){ struct tm *thetime; thetime = localtime( &tt); sprintf(timebuf, "%d%02d%02d%02d%02d%02d", thetime->tm_year + 1900, thetime->tm_mon + 1, thetime->tm_mday, thetime->tm_hour, thetime->tm_min, thetime->tm_sec); return timebuf;}//================================================================//将时间字符串(如"20030618172219")转化成time_t型的秒数time_t CFunc::StrToTime(char *buf){ time_t tt; struct tm *theTime; char tmp[5]; int tmpInt; theTime = (struct tm *)malloc(sizeof(struct tm)); memset(theTime,0,sizeof(struct tm)); if(strlen(buf)!=14) return (time_t)0; strncpy(tmp,buf,4); tmp[4]='\0'; tmpInt = atoi(tmp); if(tmpInt>2050 || tmpInt<1900) return (time_t)0; else theTime->tm_year = tmpInt - 1900; //printf("\n 111 tmpInt=[%d].\n",tmpInt); strncpy(tmp,&buf[4],2); tmp[2]='\0'; tmpInt = atoi(tmp); if(tmpInt>12 || tmpInt<1) return (time_t)0; else theTime->tm_mon = tmpInt-1; //printf("\n 222 tmpInt=[%d].\n",tmpInt); strncpy(tmp,&buf[6],2); tmp[2]='\0'; tmpInt = atoi(tmp); if(tmpInt>31 || tmpInt<1) return (time_t)0; else theTime->tm_mday = tmpInt; //printf("\n 333 tmpInt=[%d].\n",tmpInt); strncpy(tmp,&buf[8],2); tmp[2]='\0'; tmpInt = atoi(tmp); if(tmpInt>23 || tmpInt<0) return (time_t)0; else theTime->tm_hour = tmpInt; //printf("\n 444 tmpInt=[%d].\n",tmpInt); strncpy(tmp,&buf[10],2); tmp[2]='\0'; tmpInt = atoi(tmp); if(tmpInt>59 || tmpInt<0) return (time_t)0; else theTime->tm_min = tmpInt; //printf("\n 555 tmpInt=[%d].\n",tmpInt); strncpy(tmp,&buf[12],2); tmp[2]='\0'; tmpInt = atoi(tmp); if(tmpInt>59 || tmpInt<0) return (time_t)0; else theTime->tm_sec = tmpInt; //printf("\n 666 tmpInt=[%d].\n",tmpInt); theTime->tm_isdst = 0; tt = mktime(theTime); free(theTime); return tt;}//================================================================//将16进制数转化成字符串char * CFunc::HexToStr(const void *hex, int Len, void *str){ unsigned char *pHex; unsigned char *pStr; unsigned int tmp; pHex = (unsigned char *)hex; pStr = (unsigned char *)str; while(Len-- >0) { tmp = (int)(*pHex / 16); //printf("\n tmp1=%d,",tmp); if(tmp>9) //第一个字符为英文字符 { tmp = tmp -9 +64; } else //第一个字符为数字字符 tmp += 48; *pStr++ = tmp; //-------------------- tmp = (int)(*pHex % 16); //printf(" tmp2=%d,",tmp); if(tmp>9) //第一个字符为英文字符 { tmp = tmp -9 +64; } else //第一个字符为数字字符 tmp += 48; *pStr++ = tmp; pHex++; }//while *pStr = '\0'; return (char *)str;}//================================================================//将字符串转化成16进制数int CFunc::StrToHex (char * str, unsigned char *mem){ //利用定义数组,从字串直接得出数值 char array[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; int MAX = sizeof(array) /sizeof (char); int length = strlen(str); if (length %2 != 0 ) //长度不为偶数,返回错误 return -1; char * tmpStr = str; int i = 0; //用于字串定位 int j = 0; //用于数组定位 unsigned char num = 0; //用于变换字串的值 while (i < length) { j = 0; //判断传入数字是否正确,且得到位置值 while ( j< MAX && array[j] != toupper(tmpStr[i]) ) j++; if (j == MAX ) return -1; //构造变换数值 if (i % 2 == 0 ) num = j * 16; else num += j ; //如果已经为计算两次,则将值附于mem if (i % 2 != 0) { mem[i /2 ] = num ; num = 0; } i ++; } return 0;}//================================================================//编码转换函数//fromCode和toCode的取值为:"GB2312", "GBK", "Unicode","UTF-8"。。。。。等//返回值为转换后的字节数int CFunc::CodeConvert(char *fromcode, char *tocode, char *inbuf, char *outbuf, int len){ iconv_t cd; size_t status; char *inbufp, *outbufp; size_t inbytesleft, outbytesleft,inbyte; int k; inbytesleft = len; outbytesleft = inbytesleft * 2; inbyte = inbytesleft * 2; cd = iconv_open(tocode, fromcode); if ((iconv_t) (-1) == cd) { perror ("Error at iconv_open"); exit(1); } inbufp = inbuf; outbufp = outbuf; status = iconv (cd, &inbufp, &inbytesleft, &outbufp, &outbytesleft); if (status == (size_t) -1) { perror ("Error at my_iconv"); status = iconv_close(cd); if (status == (size_t) -1) { perror ("Error at iconv_close"); } } k = inbyte-outbytesleft;// if((tocode[0]=='U'||tocode[1]=='u')&&(tocode[1]=='n'||tocode[1]=='N')&&(tocode[2]=='i'||tocode[2]=='I')) // {// for(l=0;l<k-2;l++) // outbuf[l]=outbuf[l+2];// outbuf[l]='\0' ;// return(k-2);// }// else return(k); //返回转换后的字符串长度}/*int CFunc::CodeConvert(char *fromcode, char *tocode, char *inbuf, char *outbuf){ iconv_t cd; size_t status; char *inbufp, *outbufp; size_t inbytesleft, outbytesleft,inbyte; int l ,k; inbytesleft = strlen(inbuf); outbytesleft = inbytesleft * 2; inbyte = inbytesleft * 2; cd = iconv_open(tocode, fromcode); if ((iconv_t) (-1) == cd) { perror ("Error at iconv_open"); exit(1); } inbufp = inbuf; outbufp = outbuf; status = iconv (cd, &inbufp, &inbytesleft, &outbufp, &outbytesleft); if (status == (size_t) -1) { perror ("Error at my_iconv"); status = iconv_close(cd); if (status == (size_t) -1) { perror ("Error at iconv_close"); } } k = inbyte-outbytesleft; if((tocode[0]=='U'||tocode[1]=='u')&&(tocode[1]=='n'||tocode[1]=='N')&&(tocode[2]=='i'||tocode[2]=='I')) { for(l=0;l<k-2;l++) outbuf[l]=outbuf[l+2]; outbuf[l]='\0' ; return(k-2); } else return(k); //返回转换后的字符串长度}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -