当前位置:网站首页>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 .
边栏推荐
- JS settimeout() and interview questions
- Disassembling Meitu SaaS: driving the plane to change the engine
- 13.信号量临界区保护
- 面对不确定性,供应链的作用
- MYSQL关键字
- nodejs+express+mysql简单博客搭建
- [Fantasy 4] the transformation from U3D to UE4
- What are the popular frameworks for swoole in 2022?
- 2021-10-02
- Lunix reallocates root and home space memory
猜你喜欢

UVM learning - build a simple UVM verification platform

session-cookie与token

14. Code implementation of semaphore

Kustomize user manual

12.进程同步与信号量

4.随机变量

Read H264 parameters from mediarecord recording

【TS】1368- 秒懂 TypeScript 泛型工具类型!

Disassembling Meitu SaaS: driving the plane to change the engine

Flink submitter
随机推荐
VSCode工具使用
UVM factory mechanism
Mysql database remote access permission settings
Importing tables from sqoop
UVM learning - object attribute of UVM phase
The nanny level tutorial of flutter environment configuration makes the doctor green to the end
01-spooldir
MongoDB-快速上手MongoDB命令行的一些简单操作
记录 AttributeError: ‘NoneType‘ object has no attribute ‘nextcall‘
Postman -- use
PCL之K-d树与八叉树
01 install virtual machine
[visual studio] every time you open a script of unity3d, a new vs2017 will be automatically reopened
02-taildir source
What are the popular frameworks for swoole in 2022?
Redis set password
HDU1236 排名(结构体排序)
PCL Eigen介绍及简单使用
14.信号量的代码实现
Mongodb quickly get started with some simple operations of mongodb command line