当前位置:网站首页>Learn open62541 -- [66] UA_ Generation method of string
Learn open62541 -- [66] UA_ Generation method of string
2022-07-02 10:54:00 【Love is patience】
UA_String It is a special type , Its prototype is as follows , It's a structure ,
/** * 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;
Usually use C/C++ when , Strings are generally used char Type array or std::string To express , And UA_String not quite the same , Sometimes it is easy to fall into the pit when using .
This paper mainly describes UA_String Related generation methods .
relevant API
Generate UA_String Of API There are three , as follows ,
- UA_STRING
- UA_STRING_ALLOC
- UA_STRING_STATIC
Here are the introduction ,
1. UA_STRING
Its prototype is as follows ,
/** * ``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;
}
The string needs to exist in advance , Then send its address to UA_STRING(), How to use it is as follows ,
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);
It should be noted that , Because the string needs to exist in advance , Therefore, in the subsequent use, we should ensure that the space is effective , Otherwise, there will be a segment error .
2. UA_STRING_ALLOC
This is a macro definition , as follows ,
#define UA_STRING_ALLOC(CHARS) UA_String_fromChars(CHARS)
It's right UA_String_fromChars() Redefinition of , This function will copy the incoming string ( Use dynamic memory ), Will not affect the original string
UA_String ustring = UA_STRING_ALLOC("hello");
If you don't want subsequent operations, it will affect the original string , Then use this macro to generate UA_String
3. UA_STRING_STATIC
This is also a macro definition , as follows ,
/* Define strings at compile time (in ROM) */
#define UA_STRING_STATIC(CHARS) {
sizeof(CHARS)-1, (UA_Byte*)CHARS}
The string needs to exist in advance , No copying . This macro only applies to the following 2 In the form of ,
char arr[128] = "hhheee";
UA_String ustring1 = UA_STRING_STATIC(arr);
UA_String ustring2 = UA_STRING_STATIC("wwweee");
You can't write it like that ,
const char *aa = "hello";
UA_String ustring1 = UA_STRING_STATIC(aa);
Why? ? Because it uses sizeof To get the number of bytes of the string , This shows the difference between array names and pointers , Like the following example ,
char aa[128];
sizeof(aa); // be equal to 128
char *ptr = aa;
sizeof(aa); // Equal to pointer length , by 4 perhaps 8
This is a potential pit , Accidentally fell in . Again , Because the string needs to exist in advance , Therefore, in the subsequent use, we should ensure that the space is effective , Otherwise, there will be a segment error .
Summary
This article mainly describes the generation UA_String One of the three API, And precautions .
边栏推荐
- 14.信号量的代码实现
- UVM——Callback
- Kustomize使用手册
- [pit avoidance guide] pit encountered by unity3d project when accessing Tencent bugly tool
- 传输优化抽象
- Lunix reallocates root and home space memory
- Set the playback speed during the playback of UOB equipment
- Webui automated learning
- Importing tables from sqoop
- Rapid prototyping
猜你喜欢
1287_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS
VSCode工具使用
2021-10-02
[pit avoidance guide] pit encountered using ugui: the text component cannot indent the first line by two spaces
HDU1234 开门人和关门人(水题)
HDU1236 排名(结构体排序)
集成学习概览
Pytest-- test report allure configuration
拆解美图SaaS:开着飞机换引擎
Flink submitter
随机推荐
KS009基于SSH实现宠物管理系统
4.随机变量
Sus system availability scale
Convert yv12 to rgb565 image conversion, with YUV to RGB test
JSP webshell免杀——JSP的基础
(5) Gear control setting of APA scene construction
(五)APA场景搭建之挡位控制设置
axis设备的rtsp setup头中的url不能带参
stm32和电机开发(上位系统)
Flink calculates topn hot list in real time
使用Windbg静态分析dump文件(实战经验总结)
Analysis of hot spots in AI technology industry
1287_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
Solution of mysql8 forgetting password file in Windows Environment
MySQL lethal serial question 3 -- are you familiar with MySQL locks?
高考的意义是什么
【TS】1368- 秒懂 TypeScript 泛型工具类型!
MongoDB-快速上手MongoDB命令行的一些简单操作
长投学堂上面的账户安全吗?
JS settimeout() and interview questions