LoadRunner实现:计算字符串Crc32

来源:互联网 发布:p2p网络贷款平台排名 编辑:程序博客网 时间:2024/04/29 10:23
LoadRunner实现:计算字符串Crc32


可以在LR里用头文件保存或存为C文件或附加函数,代码如下:




unsigned long Crc32Table[256];


void GetCRC32Table()
{
    unsigned long crc, i, j;
    unsigned long poly = 0xEDB88320;
    for(i=0; i<256; i++)
    {
        crc = i;
        for(j=8; j>0; j--)
        {
            if(crc & 1)
                crc = (crc >> 1) ^ poly;
            else
                crc >>= 1;
        }
        Crc32Table[i] = crc;
    }
}




void GetCrc32Long(const char *ptr, char *out)
{    
    unsigned long crc = 0xFFFFFFFF;
    //unsigned char *ptr = buffer;
    long len = strlen(ptr);
    char crcStr[64];


    GetCRC32Table();
    while(len > 0)
    {
        crc = ((crc >> 8) & 0x00FFFFFF) ^ Crc32Table[(crc ^ *ptr) & 0xFF];
        ptr++;
        len--;
    }
    //转换成16进制字符串
    itoa(crc^0xffffffff,crcStr,10);
    strcpy(out, crcStr);
}


void GetCrc32(const char *ptr, char *out)
{
    char crcStr[32];
    int i,j;
    int crcInt = atoi(out);


    GetCrc32Long(ptr, out);
    itoa(crcInt, crcStr, 16);


    //小写字符转换成大写字符
    for(i=0;i<strlen(crcStr);i++)
    {
        crcStr[i]=toupper(crcStr[i]);
    }
    strcpy(out,"");
    if (strlen(crcStr)<8)
    {
        for (j=0;j<8-strlen(crcStr);j++)
        {
            strcat(out, "0");
        }    
    }
    strcat(out, crcStr);
}




调用方式:


void main()
{
    char res[13];


    GetCrc32Long("a",res);
    lr_output_message(res);


    GetCrc32("a",res);
    lr_output_message(res);
}
结果输出:
main.c(5): -390611389
main.c(8): E8B7BE43
原创粉丝点击