LoadRunner截取字符串操作 在使用LoadRunner winsockets协议写脚本,遇到下面问题: 在接收到的查询数据库的结果中我要取红色部份用于下面的select recv buf60 769 "\x01\x00\x00\x01" "\bK" "\x00\x00\x02\x03" "def\tsystem_db\n" "tb_tsbinfo\n" "tb_tsbinfo\fTskGuidebook\fTskGuidebook\f?" "\x00" "\v" "\x00\x00\x00\x03" " " "\x00\x00\x00\x00" "A" "\x00\x00\x03\x03" "def\tsystem_db\n" "tb_tsbinfo\n" "tb_tsbinfo" "\x07" "Work_ID" "\x07" "Work_ID\f?" "\x00" "\v" "\x00\x00\x00\x03\x00\x00\x00\x00\x00" "G" "\x00\x00\x04\x03" "def\tsystem_db\n" "tb_tsbinfo\n" "tb_tsbinfo\n" "WorkNum_ID\n" "WorkNum_ID\f?" "\x00" "\v" "\x00\x00\x00\x03\x00\x00\x00\x00\x00" "I" "\x00\x00\x05\x03" "def\tsystem_db" "\x10" "tb_tskdetailinfo" "\x10" "tb_tskdetailinfo\bBatch ID" "\x02" "ID\f?" "\x00" "\v" "\x00\x00\x00\x03" "!" "\x00\x00\x00\x00" "Q" "\x00\x00\x06\x03" "def\tsystem_db" "\x10" "tb_tskdetailinfo" "\x10" "tb_tskdetailinfo\tBatchName\tBatchName\f!" "\x00" "," "\x01\x00\x00\xfd\x00\x00\x00\x00\x00" "C" "\x00\x00\x07\x03" "def\tsystem_db\n" "tb_tsbinfo\n" "tb_tsbinfo\bBlock_ID\bBlock_ID\f?" "\x00" "\v" "\x00\x00\x00\x03\x00\x00\x00\x00\x00" "I" "\x00\x00" "\b" "\x03" "def\tsystem_db\ftb_blockinfo\ftb_blockinfo\tBlockName\tBlockName\f!" "\x00\x96\x00\x00\x00\xfd\x00\x00\x00\x00\x00" "G" "\x00\x00" "\t" "\x03" "def\tsystem_db\ftb_blockinfo\ftb_blockinfo\bModel_ID\bModel_ID\f?" "\x00" "\v" "\x00\x00\x00\x03\x00\x00\x00\x00\x00\x05\x00\x00" "\n" "\xfe\x00\x00" "\"" "\x00\x15\x00\x00" "\v" "\x01" "5" "\x01" "4" "\x01" "7" "\x01" "7" "\x04" "8227" "\x01" "6" "\x03" "DAY" "\x01" "3" "\x15\x00\x00" "\f" "\x01" "5" "\x01" "4" "\x01" "7" "\x01" "7" "\x04" "8227" "\x01" "5" "\x03" "NEW" "\x01" "3" "\x18\x00\x00" "\r" "\x01" "6" "\x01" "1" "\x01" "5" "\x01" "5" "\x04" "0502" "\x01" "4" "\x06" "鏁板瓧" "\x01" "2" "\x1a\x00\x00\x0e\x01" "9" "\x01" "1" "\x02" "11" "\x02" "11" "\x04" "1601" "\x01" "4" "\x06" "鏁板瓧" "\x01" "2" "\x1a\x00\x00\x0f\x01" "9" "\x01" "1" "\x02" "11" "\x02" "11" "\x04" "1601" "\x01" "1" "\x06" "鍦板潃" "\x01" "2" "\x05\x00\x00\x10\xfe\x00\x00" "\"" "\x00" send buf61 54 "2" "\x00\x00\x00\x03" "select `BlockNO` from `tb_blockinfo` where `ID`=6" 写关联语句: lrs_save_searched_string("socket3",NULL,"Block_ID","LB/BIN=\\x01","RB/BIN=\\x03",8,0,-1); 执行发现关联的值不对。检查,发现传回的结果为: "\x01\x00\x00\x01" "\bK" "\x00\x00\x02\x03" "def\tsystem_db\n" "tb_tsbinfo\n" "tb_tsbinfo\fTskGuidebook\fTskGuidebook\f?" "\x00" "\v" "\x00\x00\x00\x03" " " "\x00\x00\x00\x00" "A" "\x00\x00\x03\x03" "def\tsystem_db\n" "tb_tsbinfo\n" "tb_tsbinfo\aWork_ID\aWork_ID\f?" "\x00" "\v" "\x00\x00\x00\x03\x00\x00\x00\x00\x00" "G" "\x00\x00\x04\x03" "def\tsystem_db\n" "tb_tsbinfo\n" "tb_tsbinfo\n" "WorkNum_ID\n" "WorkNum_ID\f?" "\x00" "\v" "\x00\x00\x00\x03\x00\x00\x00\x00\x00" "I" "\x00\x00\x05\x03" "def\tsystem_db" "\x10" "tb_tskdetailinfo" "\x10" "tb_tskdetailinfo\bBatch ID" "\x02" "ID\f?" "\x00" "\v" "\x00\x00\x00\x03" "!" "\x00\x00\x00\x00" "Q" "\x00\x00\x06\x03" "def\tsystem_db" "\x10" "tb_tskdetailinfo" "\x10" "tb_tskdetailinfo\tBatchName\tBatchName\f!" "\x00" "," "\x01\x00\x00\xfd\x00\x00\x00\x00\x00" "C" "\x00\x00" "\a" "\x03" "def\tsystem_db\n" "tb_tsbinfo\n" "tb_tsbinfo\bBlock_ID\bBlock_ID\f?" "\x00" "\v" "\x00\x00\x00\x03\x00\x00\x00\x00\x00" "I" "\x00\x00" "\b" "\x03" "def\tsystem_db\ftb_blockinfo\ftb_blockinfo\tBlockName\tBlockName\f!" "\x00\x96\x00\x00\x00\xfd\x00\x00\x00\x00\x00" "G" "\x00\x00" "\t" "\x03" "def\tsystem_db\ftb_blockinfo\ftb_blockinfo\bModel_ID\bModel_ID\f?" "\x00" "\v" "\x00\x00\x00\x03\x00\x00\x00\x00\x00\x05\x00\x00" "\n" "\xfe\x00\x00" "\"" "\x00\x16\x00\x00" "\v" "\x01" "2" "\x01" "3" "\x01" "2" "\x01" "2" "\x04" "8227" "\x02" "10" "\x03" "TWO" "\x01" "4" "\x15\x00\x00" "\f" "\x01" "2" "\x01" "3" "\x01" "2" "\x01" "2" "\x04" "8227" "\x01" "9" "\x03" "ONE" "\x01" "4" "\x18\x00\x00" "\r" "\x01" "3" "\x01" "1" "\x01" "1" "\x01" "1" "\x04" "0001" "\x01" "1" "\x06" "鍦板潃" "\x01" "2" "\x18\x00\x00\x0e\x01" "3" "\x01" "1" "\x01" "1" "\x01" "1" "\x04" "0001" "\x01" "4" "\x06" "鏁板瓧" "\x01" "2" "\x05\x00\x00\x0f\xfe\x00\x00" "\"" "\x00" 经观查是改变了 在 "\x04" "8227" "\x02" "10" 中\x04和\x02表示的是后面字符的位数,如是一位就是\x01,如是三位就是\x03。 这个查表返回的结果中有8列,我要取的是第一行第六列的值。 观查后将关联语句改为: lrs_save_searched_string("socket3",NULL,"Block_ID","LB/BIN=\\v","RB/BIN=\\r",7,0,-1); 取得结果为: \x012\x013\x012\x012\x048227\x0210\x03TWO\x014\x15\x00\x00\f\x012\x013\x012\x012\x048227\x019\x03ONE\x014\x18\x00\x00 我要取得的值在第六个“\”后。写语句: char *Block; int i; int k; int len;//要取值的长度 char Block_str[100]; char Block_len[5]; Block=lr_eval_string("<Block_ID>"); for(k=1;k<=6;) { i=i+1; if(Block[i]==‘x‘) k=k+1; } strncpy(Block_len,Block+i-1,4); lrs_hex_string_to_int(Block_len,1,&len); strncpy(Block_str,Block+i+3,s); lr_output_message ("Block= %s",Block_str); 取得想要的结果。整理脚本如下: char *Block; int i; int k; int len;//要取值的长度 char Block_str[100]; char Block_len[5]; lrs_save_searched_string("socket3",NULL,"Block_ID","LB/BIN=\\v","RB/BIN=\\r",7,0,-1); Block=lr_eval_string("<Block_ID>"); //取得第六个x的位置 for(k=1;k<=6;) { i=i+1; if(Block[i]==‘x‘) k=k+1; } strncpy(Block_len,Block+i-1,4);//截/x开始四位 lrs_hex_string_to_int(Block_len,1,&len);//转为int strncpy(Block_str,Block+i+3,s); lr_output_message ("Block= %s",Block_str); 在往下的脚本编写过程中发现还需要类似的工作,直接写了个函数,如下: //str,被查找的字符串;n,第几个;c查找的字符 char *strn(char *str,int n,char c){ int nlen;//要截取串的长度,十进制数 char rstr[30]="";//截取出的串 char *retu=NULL;//要返回的值 char str_nlen[5]="";//要截取串的长度,字符 int len=0;//str长度 int i=0;//第几位,计数 int k=1;//第几个,计数 // lr_output_message ("str= %s",str); len=strlen(str);//取得str的长度 //查找第n个X的位置 for(k=1;k<=n;) { i=i+1; if(str[i]==c)//判断字符是否为c k=k+1; if(i+3>len) return 0;//找到末尾条件还是不满足,返回空 } strncpy(str_nlen,str+i-1,4);//截取\x0n,即我们所要取的值的长度,字符串 // lr_output_message ("str_nlen= %s",str_nlen); lrs_hex_string_to_int(str_nlen,1,&nlen);//将str_len转为十进制数,放入len 中 // lr_output_message ("要截取的长度为= %d",nlen); strncpy(rstr,str+i+3,nlen);//截取我们所需要的数据 // lr_output_message ("rstr为= %s",rstr); retu=rstr; return retu; } 说一下strncpy函数,有两种格式 char *strncpy(char *dest, char *src, int n); char *strncpy( strtemp,str+n , m ) --strtemp字符串变量,截取后的字符串存放处 --str 字符串变量,要截取的字符串 --n ,int 型, -- str+n, 表示从第n 位开始截取字符串 --m,int型,表示截取m位
时间: 2024-12-30 04:09:40