/*
文件名:communal.h
发布版本:0.12
发布时间:2003.12.16
*/
#include <stdio.h>
#include <string.h>
char * strcpyx(const char *s1,char *s2,unsigned int StartPos,unsigned int len);
char * Replace(const char *SourStr,char *DestStr,const char *s1,const char *s2);
char * RStr(const char *SourStr,char *DestStr,unsigned int len);
char * LStr(const char *SourStr,char *DestStr,unsigned int len);
char * RTrim(const char *SourStr,char *DestStr);
char * LTrim(const char *SourStr,char *DestStr);
char * TurnStr2(const char *SourStr,char *DestStr);
char * TurnStr(char *str);
char * IntToStr(int Value,char *str);
unsigned int pos(const char *SubStr,const char *DestStr);
unsigned char istoASCs(unsigned char* desBuffer,unsigned char* srcBuffer,int len);
int StrToEBCD(char *buf,char *ucBuffer,int BufLen);
/*
功能说明:
把s1中从指定位置开始的指定长度字符串复制到s2中
参数说明:
s1:原字符串
s2:目标字符串
s1:复制的起始位置
len:复制长度
返回值:
返回取得的字符串s2
*/
char * strcpyx(const char *s1,char *s2,unsigned int StartPos,unsigned int len)
{
unsigned int i=StartPos;
for (i=StartPos;i <StartPos+len && s1[i]!= '/0 ';i++)
{
s2[i-StartPos] = s1[i];
}
s2[i-StartPos]= '/0 ';
return s2;
}
/*
功能说明:
替换字符串中指定的内容
参数说明:
SourStr:原字符串
DestStr:替换后的字符串
s1:被替换的子串
s2:替换s1的子串
返回值:
返回替换完成后的字符串
*/
char * Replace(const char *SourStr,char *DestStr,const char *s1,const char *s2)
{
//尚未完成...
int i=0;
int j=0;
DestStr[0]= '/0 ';
while (i=pos(s1,SourStr))
{
j=i;
}
return DestStr;
}
/*
功能说明:
从右端截取指定长度的字符串
参数说明:
str:原字符串
len:截取长度
返回值:
返回截取指定长度的字符串
*/
char * RStr(const char *SourStr,char *DestStr,unsigned int len)
{
int i;
int j=0;
len = len> strlen(SourStr) ? strlen(SourStr) : len;
for (i=strlen(SourStr)-len; SourStr[i] != '/0 '; i++)
{
DestStr[j]=SourStr[i];
j++;
}
DestStr[j]= '/0 ';
return DestStr;
}
/*从左端截取指定长度的字符串*/
char * LStr(const char *SourStr,char *DestStr,unsigned int len)
{
if (len> strlen(SourStr))
len=strlen(SourStr);
if (len==0)
{
*DestStr= '/0 ';
}
else
{
strcpy(DestStr,SourStr);
*(DestStr+len)= '/0 ';
}
return DestStr;
}
/*截去一个字符串的右端空格*/
char * RTrim(const char *SourStr,char *DestStr)
{
int i;
strcpy(DestStr,SourStr);
for (i=strlen(DestStr)-1;i> =0;i--)
{
if (DestStr[i]== ' ')
DestStr[i]= '/0 ';
else
break;
}
return DestStr;
}
/*截去一个字符串的左端空格*/
char * LTrim(const char *SourStr,char *DestStr)
{
unsigned int i=0;
int flag=0;
for (i=0;i <strlen(SourStr);i++)
{
if (SourStr[i]== ' ')
i++;
else
break;
}
return strcpyx(SourStr,DestStr,i,strlen(SourStr)-i);
}
/*前后翻转一个字符串*/
char * TurnStr2(const char *SourStr,char *DestStr)
{
int i;
int len=strlen(SourStr);
for (i=0;i <len;i++)
{
*(DestStr+i)=*(SourStr+len-i-1);
}
*(DestStr+len)= '/0 ';
return DestStr;
}
/*前后翻转一个字符串*/
char * TurnStr(char *str)
{
char temp;
int i=0;
int j=strlen(str)-1;
for (i=0;i <j;i++,j--)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
return str;
}
/*把一个整数转化为一个字符串*/
char * IntToStr(int Value,char *str)
{
int i=0;
int minus=0;//负号
if (Value==0)
{
str[i]= '0 ';
i++;
}
if (Value <0)
{
minus=1;
Value=abs(Value);
}
while (Value> =1)
{
switch (Value % 10)
{
case 0:str[i]= '0 ';break;
case 1:str[i]= '1 ';break;
case 2:str[i]= '2 ';break;
case 3:str[i]= '3 ';break;
case 4:str[i]= '4 ';break;
case 5:str[i]= '5 ';break;
case 6:str[i]= '6 ';break;
case 7:str[i]= '7 ';break;
case 8:str[i]= '8 ';break;
case 9:str[i]= '9 ';break;
}
Value=Value/10;
i++;
}
if (minus)
{
str[i]= '- ';
i++;
}
str[i]= '/0 ';
return TurnStr(str);
}
/*
判断SubStr是否包含在DestStr中,
存在返回SubStr第一次出现的位置,不存在返回0
*/
unsigned int pos(const char *SubStr,const char *DestStr)
{
unsigned int i,j;
for (i=0;i <strlen(DestStr);i++)
{
if (DestStr[i]==SubStr[0])
{
for (j=0;j <strlen(SubStr);j++)
{
if (j==(strlen(SubStr)-1))
return i+1;
else if (DestStr[i+j]!=SubStr[j])
break;
}
}
}
return 0;
}
//十六进制转换为压缩BCD码
// convert {0x4c,0x5e,0x33} to "4c5e33 "
unsigned char istoASCs(unsigned char* desBuffer,unsigned char* srcBuffer,int len)
{ int i;
char ch;
for(i=0;i <len;i++)
{
ch = srcBuffer[i] > > 4;
if((ch > = 0)&&(ch <= 9))
{
desBuffer[2*i] = ch + '0 ';
}
else if((ch > =10)&&(ch <= 15))
{
desBuffer[2*i] = ch + 55;
}
else
desBuffer[2*i] = '* ';
ch = srcBuffer[i] & 0x0F;
if((ch > = 0)&&(ch <= 9))
{
desBuffer[2*i+1] = ch + '0 ';
}
else if((ch > =10)&&(ch <= 15))
{
desBuffer[2*i+1] = ch + 55;
}
else
desBuffer[2*i+1] = '* ';
}
desBuffer[2*i] = '/0 ';
return 0;
}
//压缩BCD码转换为十六进制
//89860099---> 0x89,0x86,0x00,0x99
int StrToEBCD(char *buf,char *ucBuffer,int BufLen)
{ unsigned char temp1,temp2;
int Len=BufLen/2,i;
for (i=0;i <Len;i++)
{
temp1=buf[i*2];
if (temp1> = 'a ')
temp1=temp1 - 'a ' + 10;
if (temp1> = 'A ')
temp1=temp1 - 'A ' + 10;
if (temp1> = '0 ')
temp1=temp1- '0 ';
temp2=buf[i*2 + 1];
if (temp2> = 'a ')
temp2=temp2 - 'a ' + 10;
if (temp2> = 'A ')
temp2=temp2 - 'A ' + 10;
if (temp2> = '0 ')
temp2=temp2- '0 ';
ucBuffer[i]=((temp1&0x0f) < <4)|(temp2&0x0f);
}
return 0;
}