一,数据类型转换51Testing软件测试网Z_.AW+A)D7H0c
没有使用过C编程的LoadRunner脚本编写者会发现在数据类型转化方面比较困难。下面介绍这方面的知识。
iL {&L S/C%MQ2412661. 相似函数的输出在不同的位置51Testing软件测试网;X6w/h(r D
象很多C函数一样,使用atoi函数的结果即为返回值
a}~G:Ji t3LDl241266如intResult = atoi( charY );51Testing软件测试网5M4Prp0@y.v,R*yQ
而:itoa的返回结果为第二个参数。
}cu {(r4G241266itoa( intX, charY, 10);51Testing软件测试网/SB#XI,l$T
第一个参数是需要转换的数字,第二个参数是转换后存储的字符数组,需要注意的是数组必须定义为固定的长度,如:char chary[20];51Testing软件测试网7MC-Y4ZUiBWf+[f)D
数组的最大长度为32064(32K),否则会出现“too many variables”编译错误。
1EA1N!In'M/QVM241266如果定义为变长的字符串如char *charY,则程序会出错。
V)?_b2C%S241266 第三个参数不是数组的长度,而是数字的基数,10进制是最常用的,其他还有二进制,八进制,十六进制。51Testing软件测试网Cz3`:WU |
2. 有一些函数实现了同样的功能
P,I?u| dk_;s241266itoa不是一个标准的ANSI C函数但是是C的stdlib.h中的一个函数。所以它不被包括在unix机器上的LibC中。我们可以使用标准的sprintf函数来代替:51Testing软件测试网Efkd.f
sprintf(charY,“%d”,intX);
?(?xsh$l2412663. 是用%X来转换一个十六进制数51Testing软件测试网3E6w cp5h1~q8^
int intNum;51Testing软件测试网L;F#S!xK9c
sscanf(“ffff”,“%X”,&Num);51Testing软件测试网}aPYx5^
lr_output_message(“%d”,intNum); //打印65535 ,ffff的整数值
KU%W cK[3R,Dm2412664. 从文本中提取数字的规则51Testing软件测试网aQY!S xy7`8d3A-D c
如果第一个字符不是数字或者为空,atoi返回0,即“e24”会返回051Testing软件测试网l&_0H&q4kk
atoi转换一个非数字的字符会返回组成这个字符的数字,如“-3.2”返回-3.0。“123XXX345”返回123。51Testing软件测试网3JeG4Jy_ y ^5VyM
5. LoadRunner脚本中的参数必须转换成C字符串。有两种方式来转化LR的参数为C语言的数字。
sOI3m0Q3X A;o;\$@:w241266 i = atoi( lr_eval_string("{pX}") );51Testing软件测试网RKh*P6j.K
sprintf( intX, "%d", lr_eval_string("{pX}") );51Testing软件测试网x E(^/N^2[0IX
6. 参数的算术运算
H~r1c^0a9ij241266LoadRunner没有提供对参数的算术运算的函数。所以LR的参数必须:51Testing软件测试网 e)v!D"P6T K^
1) 转换成C的整数
](LpZpq jw2412662) 使用C的函数来运算最后返回一个C的字符串51Testing软件测试网mHjc jj
3) 把返回的字符串保存成参数51Testing软件测试网WYCg^c8S[b
char cBuf[10];51Testing软件测试网'e s&[&\7@3Q~`
int i;51Testing软件测试网0tN N?&_.H@
// 1. Evaluate parameter into a C integer:
+UkM*m&c#J jh2C2fW241266i = atoi( lr_eval_string("{pNum_in}") );51Testing软件测试网 iy N{r$L
// 2. Do the math and output the result to a C string:
~R$W? r l2v241266sprintf( cBuf, "%d", i+1);
5r4P!z5L0h%w241266// 3. Save the string as a parameter to be passed on:
3^'zM)M(C{z5j/bN7B241266lr_save_string( cBuf, "pNum_out");51Testing软件测试网(x1D ]`"}M
//Print out the parameter value after incrementing it.51Testing软件测试网}!xQ+hl1weW
lr_message("**** Parameter from %s to %s",
4Lg}rf7dM[c241266 lr_eval_string("{pNum_in}"));
M3xE`*U*VMM Q zL241266 lr_eval_string("{pNum_out}"));51Testing软件测试网qFP1Z/O1M
zibeike注:除了对于数字类型的参数的运算之外,对于文本形式的参数的操作,可以参考我的另一篇文章的内容:http://www.51testing.com/?34866/action_viewspace_itemid_75592.html51Testing软件测试网Axy ?7}:l1~
二.字符串操作51Testing软件测试网.`-t a9OS
在C语言中,字符串是固定长度的,因为他们本身由独立的字符组成的字符数组。数组是只读的。任何修改字符串长度的函数调用都会报错:51Testing软件测试网f { ~*Z`g!\%V*G
)gdP9k&a241266Error: "C interpreter runtime error - memory violation error during replay.51Testing软件测试网%Zp6zT%x Ol2N*W
在LoadRunner的as_web.h库中的字符串函数可以使用“prototyping”声明的方式读写内存:
G9}(w6z7V:pDr8M241266char *strtok(char *, char *); // tokenizer prototype
char *strstr(char *, char *); // substring prototype
char *strdup(char *); // String duplication prototype
float atof(); // alpha to return float datatype
#include "as_web.h"
char *strtok(char *, char *); // prototype function call.
ActionX()
{
char aBuffer[256]; // input string to be parsed.
char *cToken; // individual token from strtok.
char cSeparator[] = " "; // blank separator.
int i; // incrementer
char val[3][20]; // output array of strings.
char modified_val[20];
// Create a parameter named pDate:
lr_save_string("January 2, 2001", "pDate");
// Put parameter into a string buffer:
strcpy( aBuffer,lr_eval_string("{pDate}"));
// Show the buffer for debugging:
lr_output_message("%s\n",aBuffer);
// get first word (to the first blank):
cToken = strtok( aBuffer,cSeparator);
i = 1;
if(!token) { // first token was not found:
lr_output_message("No tokens found in string!");
return( -1 );
} else {
while( cToken != NULL) { // tokens are not NULL:
lr_output_message("Token=%s", cToken);
// Stuff in another array:
strcpy( val[i], cToken );
// Get next token:
cToken =strtok( NULL, cSeparator);
i++; // increment
}
lr_output_message("Val #1 is: %s", val[1]);
lr_output_message("Val #2 is: %s", val[2]);
lr_output_message("Val #2 is: %s", val[3]);
strncpy( modified_val, val[2], 1 );
modified_val[2] = '\0';
while (modified_val[2] != NULL) {
lr_output_message("===>%s", modified_val);
modified_val[2] = strtok(NULL, " ");
}
}
return 0;
}
strcat连接两个字符串
7@%J#b{gJ^}241266strchr返回指向第一个要查找的字符出现的位置的指针
_K^/d;W0q ?r241266strcmp比较两个字符
5e;TFHc241266strcpy复制字符串到另一个
#s \~%]7nrjPl241266stricmp执行一个大小写敏感的比较
0scWEkh ?,KRc241266其他还有strdup,strncat,strncpy,strnicmp,strrchr,strset,strspn,strstr等字符串操作的函数。