当前位置:网站首页>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 .
边栏推荐
- 13.信号量临界区保护
- 转换YV12到RGB565图像转换,附YUV转RGB测试
- Record attributeerror: 'nonetype' object has no attribute 'nextcall‘
- MySQL数据库远程访问权限设置
- The nanny level tutorial of flutter environment configuration makes the doctor green to the end
- UVM - usage of common TLM port
- 01-spooldir
- Pytest learning --base
- Set the playback speed during the playback of UOB equipment
- VSCode工具使用
猜你喜欢
随机推荐
AttributeError: type object ‘Image‘ has no attribute ‘fromarray‘
Leetcode+ 76 - 80 storm search topic
Read H264 parameters from mediarecord recording
Session cookies and tokens
使用sqlcipher打开加密的sqlite方法
The URL in the RTSP setup header of the axis device cannot take a parameter
Webui automated learning
Oracle 笔记
LeetCode+ 76 - 80 暴搜专题
axis设备的rtsp setup头中的url不能带参
数据库字典Navicat自动生成版本
02-taildir source
Pywin32 opens the specified window
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?
session-cookie与token
What is the significance of the college entrance examination
Use WinDbg to statically analyze dump files (summary of practical experience)
Set the playback speed during the playback of UOB equipment
sqoop的表的导入
快速做出原型



![2.hacking-lab脚本关[详细writeup]](/img/f3/29745761cd5ad4df84c78ac904ea51.png)

![[SUCTF2018]followme](/img/63/9104f9c8bd24937b0fc65053efec96.png)

