📄 gsmpdu7bit.c
字号:
void IntToHexString(short int len, char *slen)
{
char hexchar[]={'0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F'};
sprintf(slen, "%c%c",hexchar[(len & 0xF0)>>4],hexchar[len & 0x0F]);
}
void HexStrToInt(char *str, int * result)
{
//char HexChars[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int high=0;
int low=0;
if ((str[0]>='0') && (str[0]<='9')) high=str[0]-'0';
if ((str[0]>='A') && (str[0]<='F')) high=str[0]-'A'+10;
if ((str[0]>='a') && (str[0]<='f')) high=str[0]-'a'+10;
if ((str[1]>='0') && (str[1]<='9')) low=str[1]-'0';
if ((str[1]>='A') && (str[1]<='F')) low=str[1]-'A'+10;
if ((str[1]>='a') && (str[1]<='f')) low=str[1]-'a'+10;
*result=high*16+low;
}
void EncodePDU7BitData(char* input, char*output)
{
int pos=0;
int c1, c2, r, temp;
int i;
int inputlen;
inputlen=StrLen(input);
IntToHexString(inputlen, output+pos);
pos+=2;
for (i=0;i<inputlen;i++)
{
if (i==inputlen)
c2=0;
else
c2=input[i+1] & 0x7F;
c1=input[i] & 0x7F;
r=(i+1) % 8;
if (r!=0)
{
temp=((c1 >> (r - 1)) | (c2 << (8-r)))& 0xFF;
IntToHexString(temp, output+pos);
pos+=2;
}
}
}
void DecodePDU7BitData(char* input, char*output)
{
int len, temp;
char tempstr1[3], tempstr2[3];
unsigned int tempval,i, j, pos;
int temp1, temp2;
int inputlen;
inputlen=0;
while ((input[inputlen]!=10)&&(input[inputlen]!=13)) inputlen++;
pos=0;
HexStrToInt(input, &len);
j=2;
for (i=1;i<=((inputlen-2) /2);i++)
{
if ((i %7)==1)
{
HexStrToInt(input+j, &temp);
output[pos]=(temp & 0x7F);
//if (output[pos]==0) output[pos]=' ';
pos++;
}
else if ((i % 7) ==0)
{
tempstr1[0]=input[j];tempstr1[1]=input[j+1];tempstr1[2]=0;
tempstr2[0]=input[j-2];tempstr2[1]=input[j-1];tempstr2[2]=0;
HexStrToInt(tempstr1, &temp1);
HexStrToInt(tempstr2, &temp2);
tempval=temp1*0x100+temp2;
output[pos]=(tempval>>2)& 0x7F;
//if (output[pos]==0) output[pos]=' ';
pos++;
output[pos]=(tempval>>9)&0x7F;
//if (output[pos]==0) output[pos]=' ';
pos++;
}
else
{
tempstr1[0]=input[j];tempstr1[1]=input[j+1];tempstr1[2]=0;
tempstr2[0]=input[j-2];tempstr2[1]=input[j-1];tempstr2[2]=0;
HexStrToInt(tempstr1, &temp1);
HexStrToInt(tempstr2, &temp2);
tempval=temp1*0x100+temp2;
output[pos]=(tempval >> (9 - (i % 7))) & 0x7F;
//if (output[pos]==0) output[pos]=' ';
pos++;
}
j+=2;
}
output[len]=0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -