Loadrunner脚本编程(4)-数据类型操作和字符串操作
http://www.360doc.com/content/10/0806/13/1698198_44078277.shtml
一,数据类型转换
没有使用过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) 把返回的字符串保存成参数
view plaincopy to clipboardprint?
char cBuf[10];
int i;
// 1. 转换成C的整数:
i = atoi( lr_eval_string("{pNum_in}") );
// 2. 使用C的函数来运算最后返回一个C的字符串:
sprintf( cBuf, "%d", i+1);
// 3.把返回的字符串保存成参数:
lr_save_string( cBuf, "pNum_out");
//Print out the parameter value after incrementing it.
lr_message("**** Parameter from %s to %s",
lr_eval_string("{pNum_in}"));
lr_eval_string("{pNum_out}"));
char cBuf[10];
int i;
// 1. 转换成C的整数:
i = atoi( lr_eval_string("{pNum_in}") );
// 2. 使用C的函数来运算最后返回一个C的字符串:
sprintf( cBuf, "%d", i+1);
// 3.把返回的字符串保存成参数:
lr_save_string( cBuf, "pNum_out");
//Print out the parameter value after incrementing it.
lr_message("**** Parameter from %s to %s",
lr_eval_string("{pNum_in}"));
lr_eval_string("{pNum_out}"));
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 prototypechar *strstr(char
*, char *); // substring prototypechar *strdup(char *); // String
duplication prototypefloat 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字符串
下面的例子使用了上面这些函数:
view plaincopy to clipboardprint?
Actions()
{
char MyString1[20] = "";
char MyString2[20] = "";
char MyString3[20] = "Mercury2";
char Cstring[10] = "12345";
int Cint;
// MyString1 is empty
//
lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);
// copy "Mercury1" into MyString1
//
strcpy(MyString1,"Mercury1");
// Now MyString1 contains "Mercury1"
//
lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);
// Copy MyString3 into MyString2
//
lr_output_message(">>>>>>>>>> MyString2 = %s",MyString2);
strcpy(MyString2,MyString3);
lr_output_message(">>>>>>>>>> MyString2 = %s",MyString2);
// Catenate MyString2 to MyString1
//
strcat(MyString1,MyString2);
lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);
// Cstring is converted to integer Cint
//
lr_output_message(">>>>>>>>>> Cstring = %s",Cstring);
Cint = atoi(Cstring);
lr_output_message(">>>>>>>>>> Cint = %d",Cint);
// Cint is converted to string
Cint = 100;
itoa(Cint,Cstring,10);
lr_output_message(">>>>>>>>>> Cstring = %s",Cstring);
return 0;
}
Actions()
{
char MyString1[20] = "";
char MyString2[20] = "";
char MyString3[20] = "Mercury2";
char Cstring[10] = "12345";
int Cint;
// MyString1 is empty
//
lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);
// copy "Mercury1" into MyString1
//
strcpy(MyString1,"Mercury1");
// Now MyString1 contains "Mercury1"
//
lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);
// Copy MyString3 into MyString2
//
lr_output_message(">>>>>>>>>> MyString2 = %s",MyString2);
strcpy(MyString2,MyString3);
lr_output_message(">>>>>>>>>> MyString2 = %s",MyString2);
// Catenate MyString2 to MyString1
//
strcat(MyString1,MyString2);
lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);
// Cstring is converted to integer Cint
//
lr_output_message(">>>>>>>>>> Cstring = %s",Cstring);
Cint = atoi(Cstring);
lr_output_message(">>>>>>>>>> Cint = %d",Cint);
// Cint is converted to string
Cint = 100;
itoa(Cint,Cstring,10);
lr_output_message(">>>>>>>>>> Cstring = %s",Cstring);
return 0;
}
//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.
Loadrunner脚本编程(4)-数据类型操作和字符串操作的更多相关文章
- loadrunner 脚本开发-int型变量和字符串的相互转换
脚本开发-int型变量和字符串的相互转换 by:授客 QQ:1033553122 字符串转化为int型变量 Action2() { int j = 0; j = atoi("12345&qu ...
- java 字符+操作,字符串+操作
字符额 “+” 操作 是拿字符在计算机底层对应的数值来进行计算的 ‘A’ = 65 A-Z是连续的 'a' = 97 a-z是连续的 '0' = 48 0-9是连续的 算数表达式中包含多个基本数据类型 ...
- loadrunner 脚本开发-调用java jar文件远程操作Oracle数据库测试
调用java jar文件远程操作Oracle数据库测试 by:授客 QQ:1033553122 测试环境 数据库:linux 下Oracle_11g_R2 Loadrunner:11 备注:想学ora ...
- Loadrunner脚本编程(2)-VuGen脚本文件的开发过程
http://www.360doc.com/content/10/0806/13/1698198_44076570.shtml 1.定义测试项目的目标,环境,脚本,测试数据,硬件等.脚本应该符合编码规 ...
- Loadrunner脚本编程(1)-大体思路
http://www.360doc.com/content/10/0806/13/1698198_44076570.shtml 就目前的了解.Loadrunner的脚本语言其实和C没什么区别.他内部的 ...
- Loadrunner脚本编程(3)- 检查点,关联等函数
http://www.360doc.com/content/10/0806/13/1698198_44078093.shtml 1. 错误预防和恢复 参数默认是用{}括起来的,但也可以指定用< ...
- python基础操作_字符串操作_列表操作list
#字符串可以通过下表取值,如下程序 names='java python' print(names[0],names[5]) #使用for循环轮询所有name值 ''' for name in nam ...
- POJ C++程序设计 编程题#4 字符串操作
编程题#4: 字符串操作 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 给 ...
- matlab字符串操作总结
matlab字符串操作总结 字符串操作总结 char(S1,S2,…)利用给定的字符串或单元数组创建字符数组double(S)将字符串转化成ASC码形式cellstr(S)利用的给定的字符数组创建字符 ...
随机推荐
- MVC使用TempData跨控制器传递信息而无需记住key的名称
通常情况下,使用TempData需要记住key的名称,本篇体验:通过帮助类,实现对TempData的设置.获取.删除. 关于传递信息的类: namespace MvcApplication1.Mode ...
- OLEDB Excel 与C# 的数据流通方法
一. 名词解释: OleDbCommand 是对数据源执行各种操作的SQL语句或者存储过程,连接access.excel等数据文件进行数据操作时候用到的,其实和sqlcomma ...
- Python之定义可变参数
如果想让一个函数能接受任意个参数,我们就可以定义一个可变参数: def fn(*args): print args 可变参数的名字前面有个 * 号,我们可以传入0个.1个或多个参数给可变参数: ...
- java编码问题总结
第一篇:JAVA字符编码系列一:Unicode,GBK,GB2312,UTF-8概念基础 第二篇:JAVA字符编码系列二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换 第三篇:J ...
- Windows Server 2003 IIS设置完全篇
一.启用Asp支持Windows Server 2003 默认安装,是不安装 IIS 6 的,需要另外安装.安装完 IIS 6,还需要单独开启对于 ASP 的支持. 第一步,启用Asp,进入:控制面板 ...
- Saving HDU hdu
话说上回讲到海东集团面临内外交困.公司的元老也仅仅剩下XHD夫妇二人了.显然.作为多年拼搏的商人,XHD不会坐以待毙的. 一天,当他正在苦思冥想解困良策的时候.突然想到了自己的传家宝,那是公司成立的时 ...
- SQL调用WebService接口
今天在做一个非常奇葩的东西.中间有个过程要在SQL触发器里面调用webservice接口.呵呵~ ALTER TRIGGER tgr_UpdateMemcached ON dbo.[User] AFT ...
- 第六章 HashSet源码解析
6.1.对于HashSet需要掌握以下几点 HashSet的创建:HashSet() 往HashSet中添加单个对象:即add(E)方法 删除HashSet中的对象:即remove(Object ke ...
- VMware vCenter中, 如何辩认虚机上Raw Device Mapping过了的一块物理磁盘?
比如说, 我们有一套VMware的环境, 其中有一台运行者ESXi的主机, 其上有十块SAS盘. 这十块盘中的五块盘被RDM到一台虚机上了. 假设你发现有添加多了一块盘, 你想移除掉, 但是5块盘其中 ...
- Anagrams leetcode java
题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...