当前位置:网站首页>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 .
边栏推荐
- [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
- Convert yv12 to rgb565 image conversion, with YUV to RGB test
- Transport Optimization abstraction
- 2021-10-02
- "Matching" is true love, a new attitude for young people to make friends
- AttributeError: type object ‘Image‘ has no attribute ‘fromarray‘
- PCL 从一个点云中提取一个子集
- Kustomize user manual
- PCL之K-d树与八叉树
- Record attributeerror: 'nonetype' object has no attribute 'nextcall‘
猜你喜欢
随机推荐
[MySQL] an exception occurs when connecting to MySQL: connection must be valid and open
01 install virtual machine
[visual studio] every time you open a script of unity3d, a new vs2017 will be automatically reopened
Record attributeerror: 'nonetype' object has no attribute 'nextcall‘
【TS】1368- 秒懂 TypeScript 泛型工具类型!
Lunix reallocates root and home space memory
Pywin32打开指定窗口
07 data import sqoop
记录 AttributeError: ‘NoneType‘ object has no attribute ‘nextcall‘
Convert yv12 to rgb565 image conversion, with YUV to RGB test
UVM learning - build a simple UVM verification platform
Shutter - canvas custom graph
Dialogue Wu Gang: why do I believe in the rise of "big country brands"?
[SUCTF2018]followme
[unity3d] cannot correctly obtain the attribute value of recttransform, resulting in calculation error
Excuse me, is it cost-effective to insure love life patron saint 2.0 increased lifelong life insurance? What are the advantages of this product?
MySQL lethal serial question 3 -- are you familiar with MySQL locks?
(5) Gear control setting of APA scene construction
axis设备的rtsp setup头中的url不能带参
AI技术产业热点分析









