如何将英文的字符串转换成UTF-8格式的字符串?函数
可使用lr_convert_string_encoding函数将字符串从一种编码手动转换为另外一种编码(UTF-八、Unicode或本地计算机编码)。编码
该函数的语法以下。spa
lr_convert_string_encoding(char * sourceString, char * fromEncoding, char * toEncoding, char * paramName)3d
该函数将结果字符串(包括其终止NULL)保存在第四个参数paramName中。若是成功,则返回0;失败,则返回−1。日志
fromEncoding和toEncoding参数的格式以下。code
LR_ENC_SYSTEM_LOCALE NULLblog
LR_ENC_UTF8 "utf-8"utf-8
LR_ENC_UNICODE "ucs-2"文档
在如下示例中,lr_convert_string_encoding将英文“Hello world”和字符串“我爱LR”由系统本地环境转换为Unicode,脚本代码以下。字符串
Action()
{
int rc = 0;
rc= lr_convert_string_encoding("Hello world", 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\"失败!");
}
return 0;
}
若是在“Run-time Settings”日志页启用了“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”字符串。