美文网首页
一段值钱的代码

一段值钱的代码

作者: Jaesoon | 来源:发表于2020-03-05 22:36 被阅读0次

    这是一段值钱的代码。能自动解析指定格式的数据。嘻嘻嘻嘻,至少这段程序为我挣了钱。

    #include <stdio.h>
    #include <math.h>
    
    void myCpy(unsigned char *desc,unsigned char *src,unsigned int start){
        for(unsigned int i=start;i>0;i--){
            *desc++ = *src++;
        }
    }
    
    #pragma  pack(push)  //保存对齐状态
    #pragma  pack(1)
    typedef struct DP{
        unsigned char dp;
        unsigned char type;
        unsigned short int len;
        unsigned char *data;
    }Dp;
    #pragma  pack(pop)
    
    unsigned int sizeofDp(Dp *thisDp){
       return thisDp->len+4;
    }
    
    unsigned long int getDpValue(Dp *thisDp){
        double value = 0;
        unsigned char *data = &thisDp->data;
        for(int i = 0; i<thisDp->len; i++){
            value += data[i]*pow(256.0,thisDp->len-1-i);
        }
        return (unsigned long int)value;
    }
    
    void dp_excutor(Dp *thisDp){
        printf("thisDp:\n\t dp:\t%d \n\ttype:\t%d \n\tlen:\t%d \n\tvalue:\t%d \n",thisDp->dp,thisDp->type,thisDp->len,getDpValue(thisDp));
        switch(thisDp->dp){
            case 1:
                break;
            case 2:
                break;
        }
    }
    
    typedef int (*fun_excutor)(Dp *thisDp);
    unsigned int parser(unsigned char cmd[],unsigned int len,unsigned int startPosition  , fun_excutor dp_excutor){
        unsigned char myCmd[len-startPosition];
        unsigned char temp = 0;
        Dp *thisDp;
        myCpy(myCmd, cmd+startPosition, len-startPosition);
        temp  = myCmd[2];
        myCmd[2] = myCmd[3];
        myCmd[3] = temp;
        thisDp = (Dp *)((unsigned char*)myCmd);
        printf("\n size of Dp:%d\n",sizeofDp (thisDp));
        if(dp_excutor!=NULL){
            dp_excutor(thisDp);
        }
        if(startPosition+sizeofDp(thisDp) < len){
            parser(cmd,len,startPosition+sizeofDp(thisDp),(fun_excutor)dp_excutor);
        }
        return startPosition+sizeofDp(thisDp);
    }
    
    int main()
    {
        unsigned char cmd[]= {10,4,0,1,1,11,2,0,4,0,0,1,244,8,2,0,4,0,0,0,10};
        unsigned int cmdLen = sizeof (cmd);
        parser(cmd,cmdLen,0,(fun_excutor)dp_excutor);
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:一段值钱的代码

          本文链接:https://www.haomeiwen.com/subject/qswlrhtx.html