一,数据类型转换
没有使用过C编程的LoadRunner脚本编写者会发现在数据类型转化方面比较困难。下面介绍这方面的知识。
1. 相似函数的输出在不同的位置
象很多C函数一样,使用atoi函数的结果即为返回值
如intResult = atoi( charY );
而:itoa的返回结果为第二个参数。
itoa( intX, charY, 10);
第一个参数是需要转换的数字,第二个参数是转换后存储的字符数组,需要注意的是数组必须定义为固定的长度,如:char chary[20];
数组的最大长度为32064(32K),否则会出现“too many variables”编译错误。
如果定义为变长的字符串如char *charY,则程序会出错。
第三个参数不是数组的长度,而是数字的基数,10进制是最常用的,其他还有二进制,八进制,十六进制。
2. 有一些函数实现了同样的功能
itoa不是一个标准的ANSI C函数但是是C的stdlib.h中的一个函数。所以它不被包括在unix机器上的LibC中。我们可以使用标准的sprintf函数来代替:
sprintf(charY,“%d”,intX);
sprintf
Writes formatted output to a string.
3. 是用%X来转换一个十六进制数
int intNum;
sscanf(“ffff”,“%X”,&Num);
lr_output_message(“%d”,intNum); // 打印65535 ,ffff的整数值
sscanf
Reads formatted input from a string.
4. 从文本中提取数字的规则
如果第一个字符不是数字或者为空,atoi返回0,即“e24”会返回0
atoi 转换一个非数字的字符会返回组成这个字符的数字,如“-3.2”返回-3.0。“123XXX345”返回123。
Converts a string to an integer value.
atoi reads the initial portion of the string only, by stopping at the first non-numerical character.
5. LoadRunner脚本中的参数必须转换成C字符串。有两种方式来转化LR的参数为C语言的数字。
i = atoi( lr_eval_string("{pX}") );
sprintf( intX, "%d", lr_eval_string("{pX}") );
lr_eval_string
Returns the string argument after evaluating embedded parameters.
The lr_eval_string function returns the input string after evaluating any embedded parameters. If string argument contains only a parameter, the function returns the current value of the parameter.
Embedded parameters must be in brackets.
6. 参数的算术运算
LoadRunner没有提供对参数的算术运算的函数。所以LR的参数必须:
1) 转换成C的整数
2) 使用C的函数来运算最后返回一个C的字符串
3) 把返回的字符串保存成参数
zibeike注:除了对于数字类型的参数的运算之外,对于文本形式的参数的操作,可以参考我的另一篇文章的内容:http://www.51testing.com/?34866/action_viewspace_itemid_75592.html
二.字符串操作
在C语言中,字符串是固定长度的,因为他们本身由独立的字符组成的字符数组。数组是只读的。任何修改字符串长度的函数调用都会报错:
Error: "C interpreter runtime error - memory violation error during replay.
在LoadRunner的as_web.h库中的字符串函数可以使用“prototyping”声明的方式读写内存:
char *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];
// 创建一个参数pDate:
lr_save_string("January 2, 2001", "pDate");
// 把参数放到字符串缓冲Put parameter into a string buffer:
//strcpy:Copies one string to another.
//lr_eval_string:Returns the string argument after evaluating embedded parameters.
strcpy( aBuffer,lr_eval_string("{pDate}"));
//在调试中显示这个buffer Show the buffer for debugging:
//lr_output_message:Sends a message to the log file and Output window
lr_output_message("%s/n",aBuffer);
// get first word (to the first blank):
//strtok:Returns a token from a string delimited by specified characters.
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 连接两个字符串
strchr 返回指向第一个要查找的字符出现的位置的指针
strcmp 比较两个字符
strcpy 复制字符串到另一个
stricmp 执行一个大小写敏感的比较
其他还有strdup,strncat,strncpy,strnicmp,strrchr,strset,strspn,strstr等字符串操作的函数。
zibeike注:关于更多字符串操作的脚本编写,可以参考我的另一篇文章:
http://www.51testing.com/?34866/action_viewspace_itemid_75428.html
zibeike翻译自:http://www.wilsonmar.com/1lrscrīpt.htm
LoadRunner中常用的字符串操作函数有:
strcpy(destination_string, source_string);
strcat(string_that_gets_appended, string_that_is_appended);
atoi(string_to_convert_to_int); //returns the integer value
itoa(integer_to_conver_to_string, destination_string, base); // base is 10
strcmp(string1, string2); // returns 0 if both strings are equal
对各函数的定义:
strcpy( ):拷贝一个字符串到另一个字符串中.
strcat( ):添加一个字符串到另一个字符串的末尾。
strcmp( ):比较两个字符串,如果相等返回0。
atoi():转换一个ASCII字符串为一个整型。
itoa():根据给定的进制,转换一个整型数据为ASCII字符串
下面的例子使用了上面这些函数:
//To send an error message to the LoadRunner output window or Application Management agent log,
use the lr_error_message function.
It is not recommended that you send a message to the output window or agent log in the middle of a transaction,
as it will lengthen the execution time. To send a message to the Vuser execution log or Application Management Web site,
but not to the Output window, use lr_log_message.