LoadRunner 如何将英文的字符串转换成UTF-8格式的字符串?

时间:2023-01-05 21:59:01

LoadRunner 如何将英文的字符串转换成UTF-8格式的字符串?

1.问题提出

如何将英文的字符串转换成UTF-8格式的字符串?

2.问题解答

可以使用lr_convert_string_encoding函数将字符串从一种编码手动转换为另一种编码(UTF-8Unicode或本地计算机编码)。

该函数的语法如下。

 

lr_convert_string_encoding(char * sourceString,char * fromEncoding, char * toEncoding, char * paramName)


该函数将结果字符串(包括其终止NULL)保存在第四个参数paramName中。如果成功,则返回0;失败,则返回1

fromEncodingtoEncoding参数的格式如下。

    LR_ENC_SYSTEM_LOCALE       NULL

    LR_ENC_UTF8               "utf-8"

    LR_ENC_UNICODE            "ucs-2"


在以下示例中,lr_convert_string_encoding将英文“Hello world”和字符串“我爱LR”由系统本地环境转换为Unicode,脚本代码如下。

 

Action()

{

    intrc = 0;

    rc=lr_convert_string_encoding("Helloworld", LR_ENC_SYSTEM_LOCALE, LR_ENC_UNICODE,

"strUnicode");

    if(rc< 0)

    {

        lr_output_message("转换\"Hello world\"失败!");

    }

    rc=lr_convert_string_encoding("我爱LR", LR_ENC_SYSTEM_LOCALE, LR_ENC_UNICODE,

"strUnicode");

    if(rc< 0)

    {

        lr_output_message("转换\"我爱LR\"失败!");

    }

    return0;

}


如果在“Run-timeSettings”日志页启用了“Extended log”组的“Parameter substitution”复选框,则在执行日志中,输出窗口将显示以下信息。

 

Running Vuser...

Starting iteration 1.

Starting action Action.

Action.c(4): Notify: Saving Parameter"strUnicode = H\x00e\x00l\x00l\x00o\x00\x00w\x00o\x00r\x00l\x00d\x00\x00\x00"

Action.c(9): Notify: Saving Parameter "strUnicode= \x11b1rL\x00R\x00\x00\x00"

Ending action Action.

Ending iteration 1.

Ending Vuser...


从上面的脚本和代码中不难看出,应用lr_convert_string_encoding()函数可以将转换后的字符保存到strUnicode变量中。“H\x00e\x00l\x00l\x00o\x00\x00w\x00o\x00r\x00l\x00d\x00\ x00\x00”这段Unicode文本对应的是“Hello world”英文文本,而“\x11b1rL\ x00R\x00\x00\x00”对应的是“我爱LR”字符串。


LoadRunner 如何将英文的字符串转换成UTF-8格式的字符串?(PDF已排版

本文出自 “测试者家园” 博客,请务必保留此出处http://tester2test.blog.51cto.com/461899/1577669