当前位置:网站首页>学习open62541 --- [66] UA_String的生成方法
学习open62541 --- [66] UA_String的生成方法
2022-07-02 07:11:00 【爱就是恒久忍耐】
UA_String是比较特殊的类型,其原型如下,是个结构体,
/** * String * ^^^^^^ * A sequence of Unicode characters. Strings are just an array of UA_Byte. */
typedef struct {
size_t length; /* The length of the string */
UA_Byte *data; /* The content (not null-terminated) */
} UA_String;
平时使用C/C++时,字符串一般使用char型数组或者std::string来表示,与UA_String稍有不同,使用时有时容易掉坑里。
本文主要讲述UA_String的相关生成方法。
相关API
生成UA_String的API有三个,如下,
- UA_STRING
- UA_STRING_ALLOC
- UA_STRING_STATIC
下面分别介绍,
1. UA_STRING
其原型如下,
/** * ``UA_STRING`` returns a string pointing to the original char-array. * ``UA_STRING_ALLOC`` is shorthand for ``UA_String_fromChars`` and makes a copy * of the char-array. */
static UA_INLINE UA_String
UA_STRING(char *chars) {
UA_String s; s.length = 0; s.data = NULL;
if(!chars)
return s;
s.length = strlen(chars); s.data = (UA_Byte*)chars; return s;
}
字符串需要事先存在,然后把其地址传给UA_STRING(),使用方法如下,
const char *aa = "hello";
UA_String ustring1 = UA_STRING((char*)aa);
std::string tt = "world";
UA_String ustring2 = UA_STRING((char*)tt.data());
char arr[128] = "hhheee";
UA_String ustring3 = UA_STRING(arr);
需要注意的是,由于需要字符串事先存在,所以在后续使用时要保证空间是有效的,否则会出现段错误。
2. UA_STRING_ALLOC
这是个宏定义,如下,
#define UA_STRING_ALLOC(CHARS) UA_String_fromChars(CHARS)
是对UA_String_fromChars()的重定义,这个函数会对传入的字符串进行拷贝(使用动态内存),不会影响原先的字符串
UA_String ustring = UA_STRING_ALLOC("hello");
如果不想后续操作会影响原先的字符串,那么就使用这个宏来生成UA_String
3. UA_STRING_STATIC
这也是个宏定义,如下,
/* Define strings at compile time (in ROM) */
#define UA_STRING_STATIC(CHARS) {
sizeof(CHARS)-1, (UA_Byte*)CHARS}
需要字符串事先存在,不会拷贝。这个宏只适用于如下2种形式,
char arr[128] = "hhheee";
UA_String ustring1 = UA_STRING_STATIC(arr);
UA_String ustring2 = UA_STRING_STATIC("wwweee");
不能这样写,
const char *aa = "hello";
UA_String ustring1 = UA_STRING_STATIC(aa);
为什么?因为其使用了sizeof来获取字符串的字节数,这就体现了数组名和指针的区别,如下面的例子,
char aa[128];
sizeof(aa); // 等于128
char *ptr = aa;
sizeof(aa); // 等于指针长度,为4或者8
这是个潜在的坑,一不小心就掉进去了。同样,由于需要字符串事先存在,所以在后续使用时要保证空间是有效的,否则会出现段错误。
小结
本文主要讲述了生成UA_String的三个API,及其注意事项。
边栏推荐
猜你喜欢
07 data import sqoop
618再次霸榜的秘密何在?耐克最新财报给出答案
Introduction to MySQL 8 DBA foundation tutorial
js数组常用方法
UVM learning - build a simple UVM verification platform
[TS] 1368 seconds understand typescript generic tool types!
"Talking about podcasts" vol.352 the age of children: breaking the inner scroll, what can we do before high school?
Post disaster reconstruction -- Floyd thought
【TS】1368- 秒懂 TypeScript 泛型工具类型!
快速做出原型
随机推荐
618再次霸榜的秘密何在?耐克最新财报给出答案
MYSQL环境配置
Kustomize使用手册
Session cookies and tokens
使用Windbg静态分析dump文件(实战经验总结)
Flink实时计算topN热榜
Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application
Pytest framework implements pre post
[jetbrain rider] an exception occurred in the construction project: the imported project "d:\visualstudio2017\ide\msbuild\15.0\bin\roslyn\microsoft.csh" was not found
PCL 从一个点云中提取一个子集
[Fantasy 4] introduction and use of UMG components (under update...)
stm32和电机开发(上位系统)
Windows环境MySQL8忘记密码文件解决方案
SQOOP 1.4.6 INSTALL
2.hacking-lab脚本关[详细writeup]
01-spooldir
使用sqlcipher打开加密的sqlite方法
07数据导入Sqoop
js promise.all
shell编程01_Shell基础