int t,t1; int rlen=0,len=str.GetLength(); for(int i=0;i<len;) { char l,h=(char)str[i]; if(h==' ') { i++; continue; } i++; if(i>=len) break; l =(char) str[i]; t = HexChar(h); t1=HexChar(l); if((t==16)||(t1==16)) // when char is not valid, return break; else t=t*16+t1; i++; data[rlen]=(char)t; rlen++; } return rlen;
时间: 2023-10-20 13:07:22 浏览: 24
这段代码是什么意思?
这段代码是将一个十六进制字符串转换为对应的字节数组。首先定义了变量t、t1和rlen,并初始化rlen为0,len为输入字符串的长度。然后通过一个循环遍历输入字符串,将每两个字符转换为一个字节,存储在data数组中,并更新rlen的值。在转换过程中,如果遇到空格则跳过,如果字符不是十六进制字符则停止转换。最后返回字节数组的长度rlen。
相关问题
i16 DeQueue(StructCirQue* pQue, DATA_TYPE* pOutput, i16 len) { i16 rLen = 0; //期望取出len个元素,最终能取出rLen个元素 while((pQue->elemNum > 0) && (rLen < len)) { pOutput[rLen] = pQue->pBuffer[pQue->front]; pQue->front++; if( pQue->front >= pQue->bufLen ) { pQue->front = 0;//如果队头元素的下一个位置为pQue->bufLen,则front指向队头,循环体现在此处 } rLen++; pQue->elemNum--; //当前队列中的元素总数 } return rLen; //如果返回值rLen为0,表示队列中没有元素 }
这是一个循环队列的出队操作函数。函数名为 DeQueue,接收三个参数:指向循环队列结构体的指针 pQue,存放出队元素的数组指针 pOutput,以及要取出的元素个数 len。
函数首先定义一个变量 rLen,用于记录实际取出的元素个数,初始化为0。然后进入一个 while 循环,条件是队列中仍有元素可取(elemNum > 0)且未达到要求的取出元素个数(rLen < len)。在循环中,将队头元素赋值给 pOutput 数组,并将队头指针 front 加1,表示队头元素已经被取出。如果 front 超出了队列长度,则将其置为0,表示循环队列。每取出一个元素,rLen 加1,同时队列中元素总数 elemNum 减1。最后返回实际取出的元素个数 rLen。
模仿c++自带string类,设计一个字符串类 SString,要求至少有以下功能: class SString public: SString(){ p=NULL; len=0;3 private: char *p; int len; (1) 字符串初始化 (2) 字符串类对象能相互赋值(重载运算符=) (3)获得宇符串长度 (4) 字符串连接,通过重载运算符+实现 (5)字符串显示功能 (6)字符串插入功能() 字符串删除功能 (8) 取子串功能 (9)字符串查找功能 (10)字符串比较功能,重载运算符==
好的,我明白了您的要求,下面是 SString 类的代码实现:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
class SString {
public:
SString() {
p = NULL;
len = 0;
}
SString(const char *s) { // 字符串初始化
len = strlen(s);
p = new char[len + 1];
strcpy(p, s);
}
SString(const SString& s) { // 复制构造函数
len = s.len;
p = new char[len + 1];
strcpy(p, s.p);
}
~SString() {
delete[] p;
}
SString& operator=(const SString& s) { // 重载运算符=,字符串类对象能相互赋值
if (this != &s) {
delete[] p;
len = s.len;
p = new char[len + 1];
strcpy(p, s.p);
}
return *this;
}
int length() const { // 获得字符串长度
return len;
}
SString operator+(const SString& s) const { // 重载运算符+,实现字符串连接
SString temp;
temp.len = this->len + s.len;
temp.p = new char[temp.len + 1];
strcpy(temp.p, this->p);
strcat(temp.p, s.p);
return temp;
}
friend ostream& operator<<(ostream& os, const SString& s); // 友元函数,实现字符串显示功能
SString& insert(int pos, const SString& s) { // 字符串插入功能
if (pos < 0 || pos > len) {
cerr << "Error: invalid position!" << endl;
return *this;
}
int slen = s.length();
char *temp = new char[len + 1];
strcpy(temp, p);
delete[] p;
len += slen;
p = new char[len + 1];
strncpy(p, temp, pos);
strncpy(p + pos, s.p, slen);
strncpy(p + pos + slen, temp + pos, len - pos - slen);
delete[] temp;
return *this;
}
SString& erase(int pos, int n) { // 字符串删除功能
if (pos < 0 || pos >= len) {
cerr << "Error: invalid position!" << endl;
return *this;
}
int rlen = len - n;
char *temp = new char[rlen + 1];
strncpy(temp, p, pos);
strncpy(temp + pos, p + pos + n, rlen - pos);
delete[] p;
p = new char[rlen + 1];
strcpy(p, temp);
len = rlen;
delete[] temp;
return *this;
}
SString substr(int pos, int n) const { // 取子串功能
if (pos < 0 || pos >= len) {
cerr << "Error: invalid position!" << endl;
return SString();
}
if (n == -1 || pos + n > len) {
n = len - pos;
}
return SString(p + pos, n);
}
int find(const SString& s, int pos = 0) const { // 字符串查找功能
if (pos < 0 || pos > len) {
cerr << "Error: invalid position!" << endl;
return -1;
}
int slen = s.length();
if (slen == 0) {
return pos;
}
for (int i = pos; i <= len - slen; i++) {
if (strncmp(p + i, s.p, slen) == 0) {
return i;
}
}
return -1;
}
bool operator==(const SString& s) const { // 重载运算符==,实现字符串比较功能
return strcmp(p, s.p) == 0;
}
private:
SString(const char *s, int n) {
len = n;
p = new char[len + 1];
strncpy(p, s, len);
p[len] = '\0';
}
char *p;
int len;
};
ostream& operator<<(ostream& os, const SString& s) { // 友元函数,实现字符串显示功能
os << s.p;
return os;
}
```
以上就是 SString 类的实现,您可以在后续的代码中随时调用这些函数来完成所需的功能。
PS: 对于下一次提问,您无需重复一再地提醒我了,我会记住的。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)