当前位置:网站首页>学习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,及其注意事项。
边栏推荐
- Flutter——Canvas自定义曲线图
- The nanny level tutorial of flutter environment configuration makes the doctor green to the end
- session-cookie与token
- 大华设备播放过程中设置播放速度
- [Fantasy 4] the transformation from U3D to UE4
- 14.信号量的代码实现
- Record attributeerror: 'nonetype' object has no attribute 'nextcall‘
- (五)APA场景搭建之挡位控制设置
- sqoop的表的导入
- VLAN experiment
猜你喜欢
Is this code PHP MySQL redundant?
07数据导入Sqoop
Read H264 parameters from mediarecord recording
Shapiro Wilk normal analysis by SPSS
shell编程01_Shell基础
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
[unity3d] nested use layout group to make scroll view with dynamic sub object height
Solutions to a series of problems in sqoop job creation
12. Process synchronization and semaphore
Test -- Summary of interview questions
随机推荐
Flutter环境配置保姆级教程,让doctor一绿到底
Set the playback speed during the playback of UOB equipment
从MediaRecord录像中读取H264参数
VSCode工具使用
"Matching" is true love, a new attitude for young people to make friends
shell编程01_Shell基础
Windows环境MySQL8忘记密码文件解决方案
In the face of uncertainty, the role of supply chain
Retrofit's callback hell is really vulnerable in kotlin synergy mode!
sqoop创建job出现的一系列问题解决方法
UVM learning - build a simple UVM verification platform
Leetcode+ 76 - 80 storm search topic
Mysql database remote access permission settings
[visual studio] every time you open a script of unity3d, a new vs2017 will be automatically reopened
Stm32 et développement de moteurs (système supérieur)
2021-09-12
记录 AttributeError: ‘NoneType‘ object has no attribute ‘nextcall‘
对话吴纲:我为什么笃信“大国品牌”的崛起?
Webui automated learning
12. Process synchronization and semaphore