51Testing软件测试网6l(Zoy [.M.D-LG0ow
一,数据类型转换51Testing软件测试网.Y{Bd_p
没有使用过C编程的LoadRunner脚本编写者会发现在数据类型转化方面比较困难。下面介绍这方面的知识。51Testing软件测试网'}l$k
`7f$W)U)j
1. 相似函数的输出在不同的位置51Testing软件测试网%LK*w0G.yj
象很多C函数一样,使用atoi函数的结果即为返回值51Testing软件测试网l9}1z$X r R!J
如intResult = atoi( charY );
\G"u1j']0而:itoa的返回结果为第二个参数。
M*~e]@4Xci0itoa( intX, charY, 10);
Z^tdH0 第一个参数是需要转换的数字,第二个参数是转换后存储的字符数组,需要注意的是数组必须定义为固定的长度,如:char chary[20];51Testing软件测试网
?l,D G.H\P{1W(je7\
数组的最大长度为32064(32K),否则会出现“too many variables”编译错误。
7[T8z)M+W$\3Y0如果定义为变长的字符串如char *charY,则程序会出错。
I\uB.Dw#\Y0 第三个参数不是数组的长度,而是数字的基数,10进制是最常用的,其他还有二进制,八进制,十六进制。51Testing软件测试网!Z)Zlk1^"se
2. 有一些函数实现了同样的功能51Testing软件测试网4Z:l+SU7jE(b+w&U
itoa不是一个标准的ANSI C函数但是是C的stdlib.h中的一个函数。所以它不被包括在unix机器上的LibC中。我们可以使用标准的sprintf函数来代替:51Testing软件测试网v uhvlXD
sprintf(charY,“%d”,intX);51Testing软件测试网R!K5QaF
3. 是用%X来转换一个十六进制数
:gs.M%l*Jio0int intNum;
x
gUKG~Wd0sscanf(“ffff”,“%X”,&Num);51Testing软件测试网Ohoqv8mW)zi
lr_output_message(“%d”,intNum); //打印65535 ,ffff的整数值51Testing软件测试网-cZ$[1y ?zA;t:Z
4. 从文本中提取数字的规则51Testing软件测试网v/eb"p^7O
如果第一个字符不是数字或者为空,atoi返回0,即“e24”会返回0
@
[)W'z.kx[Rubx-V0atoi转换一个非数字的字符会返回组成这个字符的数字,如“-3.2”返回-3.0。“123XXX345”返回123。
I&vP7b\N&x.QU7`05. LoadRunner脚本中的参数必须转换成C字符串。有两种方式来转化LR的参数为C语言的数字。51Testing软件测试网n.M/['uj R3f+G
i = atoi( lr_eval_string("{pX}") );
!\CneN1z'[R jN0sprintf( intX, "%d", lr_eval_string("{pX}") );
MTLtI[G06. 参数的算术运算51Testing软件测试网ow9o)yNi y
LoadRunner没有提供对参数的算术运算的函数。所以LR的参数必须:
J0pr2wv01) 转换成C的整数51Testing软件测试网 DD*]pt;y6@6?H{$}{
2) 使用C的函数来运算最后返回一个C的字符串51Testing软件测试网2k.| }&@s5O1Olac
3) 把返回的字符串保存成参数
)gUG:Wj0char cBuf[10];51Testing软件测试网0\Jz,V)f/r.v+_q;`
int i;
L+Q$|#t^l(?8~e,F~0// 1. Evaluate parameter into a C integer:
;R%`z,y/w/P7ulFb0i = atoi( lr_eval_string("{pNum_in}") );
5JF6HJ;D0// 2. Do the math and output the result to a C string:51Testing软件测试网&Z S
E XlK*Q
sprintf( cBuf, "%d", i+1);
,N2N0q7Y9lU0q0// 3. Save the string as a parameter to be passed on:
T`F'E L-y QlT0lr_save_string( cBuf, "pNum_out");51Testing软件测试网
ui2Oxx|)_9A
//Print out the parameter value after incrementing it.51Testing软件测试网V:M'k&so5E`
lr_message("**** Parameter from %s to %s",51Testing软件测试网#tG0F8[;d ho+zP-O
q+QX
lr_eval_string("{pNum_in}"));
-s$o/c-{8|A:N0 lr_eval_string("{pNum_out}"));51Testing软件测试网'A*o9Zn!E:~_K'Dz
zibeike注:除了对于数字类型的参数的运算之外,对于文本形式的参数的操作,可以参考我的另一篇文章的内容:http://www.51testing.com/?34866/action_viewspace_itemid_75592.html
)@ ]-ZX
G]n0二.字符串操作
%lH`.Ly~%@F0在C语言中,字符串是固定长度的,因为他们本身由独立的字符组成的字符数组。数组是只读的。任何修改字符串长度的函数调用都会报错:51Testing软件测试网4Cda0f"V
51Testing软件测试网|+G
|w$N5M!S"LError: "C interpreter runtime error - memory violation error during replay.
&KK)q Gj_0在LoadRunner的as_web.h库中的字符串函数可以使用“prototyping”声明的方式读写内存:
+F
}jk"\:jMe0char *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连接两个字符串51Testing软件测试网uY$sL0v e0I~z
strchr返回指向第一个要查找的字符出现的位置的指针
Q#pQHSE0strcmp比较两个字符51Testing软件测试网1WRN'Y:K#R(I+Y
strcpy复制字符串到另一个
.y`M{5[.RO0stricmp执行一个大小写敏感的比较51Testing软件测试网i%jvB3qS
其他还有strdup,strncat,strncpy,strnicmp,strrchr,strset,strspn,strstr等字符串操作的函数。51Testing软件测试网;Uo&Yl T
[!g
zibeike注:关于更多字符串操作的脚本编写,可以参考我的另一篇文章:
3KU(gi b3yrt(u051Testing软件测试网y&I$w$hQ@http://www.51testing.com/?34866/action_viewspace_itemid_75428.html
\'hxf"Q#o3|-n~0zibeike翻译自:http://www.wilsonmar.com/1lrscrīpt.htm
"`Qj\v0