C++ 获取指定字符数组的前n个元素
我的目标是从序列号中读取一些字符串,例如234124!3455addg#5867如果程序看到!它应该开始将它添加到一个char数组中,如果它看到#它应该返回该char数组的前4个元素,对于我的示例,返回值应该是3455。我怎样才能解决它?我使用String类实现了这一点,但我需要将其实现为char数组。 我对arduino很陌生,所以请说清楚谢谢。 这是我的密码:C++ 获取指定字符数组的前n个元素,c++,arrays,arduino,char,arduino-uno,C++,Arrays,Arduino,Char,Arduino Uno,我的目标是从序列号中读取一些字符串,例如234124!3455addg#5867如果程序看到!它应该开始将它添加到一个char数组中,如果它看到#它应该返回该char数组的前4个元素,对于我的示例,返回值应该是3455。我怎样才能解决它?我使用String类实现了这一点,但我需要将其实现为char数组。 我对arduino很陌生,所以请说清楚谢谢。 这是我的密码: const char *s = "123123123!0037selam#aaaaSDSDa"; const char *CHAR1
const char *s = "123123123!0037selam#aaaaSDSDa";
const char *CHAR1 = "!";
const char *CHAR2 = "#";
char *target = NULL;
char *start, *end;
void setup() {
Serial.begin(9600);
}
void loop() {
if ( start = strstr( s, CHAR1 ) )
{
start += strlen( CHAR1 );
if ( end = strstr( start, CHAR2 ) )
{
target = ( char * )malloc( end - start + 1 );
memcpy( target, start, end - start );
target[end - start] = '\0';
}
}
if ( target )
{
for(int i=0; i<4;i++)
Serial.print( target[i]);
}
free( target );
return 0;
}
const char*s=“123123123!0037selam#aaasadsda”;
常量字符*CHAR1=“!”;
常量char*CHAR2=“#”;
char*target=NULL;
字符*开始,*结束;
无效设置(){
Serial.begin(9600);
}
void循环(){
如果(开始=strstr(s,CHAR1))
{
start+=strlen(字符1);
如果(结束=strstr(开始,字符2))
{
target=(char*)malloc(end-start+1);
memcpy(目标、开始、结束-开始);
目标[结束-开始]='\0';
}
}
如果(目标)
{
对于(inti=0;i我遵循了您的代码,并做了一些更改,以作为您的示例
如果您的字符串总是这样,即!
总是在#
之前,并且在它们之间总是有一些数字需要处理,那么您可以执行一些循环来等待这些标记
因此,基本上:
- 循环以不断检查
!
标记的接收情况
- 循环使用
isdigit()
函数不断检查有效数字
- 循环查找结束标记,
#
但是,这个算法同样适用于您提供的示例
#定义最大尺寸(200)
字符Tmp[MAXSIZE];
无效设置(){
Serial.begin(9600);
Serial.println(“输入消息”);
}
void循环(){
int计数器,i;
收到的字符;
而(Serial.available()我认为这是一种更简单的方法。这取决于是否有未明确说明的需求
有几件事值得一提,
您希望返回“!”后面的前4个字节,因此
只需要缓冲4个字符
我现在手头没有所有的电缆,所以我只是
在电脑上运行的东西。在你的情况下,而不是
返回刚输出的字符串缓冲区的副本
连载打印
代码:
#包括
#包括
#包括
#包括
使用名称空间std;
类虚拟
{
公众:
伪()
{
const char*testData=“234124!3455addg#5867”;
int dataLen=strlen(testData);
mData=新字符[dataLen+1];
strcpy(mData、testData);
mTotal=strlen(测试数据);
mIndex=0;
}
int可用()
{
返回mTotal-mIndex;
}
char read()
{
返回mData[mIndex++];
}
私人:
char*mData;
int mIndex;
国际总公司;
};
char*testFunc()
{
虚拟*串行=新虚拟();
///-----------8<-----------在这里剪到下一把剪刀。放入循环函数中
///代码在loop()的单个迭代中执行所有功能(读取和缓冲)。
///通常,我希望每次读取一个字符。我希望loop()是
///在输出结果之前运行16次,因为#是字符串的第16个字符。
char tmpBuffer[5]={0};
int bufferIndex=0;
bool-marker1Seen=false;
而(串行->可用()>0)
{
接收字符=串行->读取();
如果(接收=='!')
{
marker1Seen=true;
}
else if(接收=='#')
{
返回strdup(tmpBuffer);
}
else if(marker1Seen==true&&bufferIndex<4)
{
tmpBuffer[bufferIndex++]=已接收;
}
}
//如果输入格式正确,则不应到达此处
返回NULL;
///-----------8<-----------在此处切割
}
int main()
{
char*result=testFunc();
CUT你已经提供了一些代码,但是你没有解释它有什么问题。注意ARDUNO使用C++而不是C。我的问题是它不能完全工作。我不能做任何有意义的事情,所以我请求Queston帮助。它不起作用。应该有串行。打印()我不明白。当我输入234124!3455addg#5867时,程序应该返回3455,但当我运行它时,你的代码没有任何作用。让问题更容易理解。我想得到两个字符之间的字符,它们是!和#首先我编辑以将其打印到序列what它存储在Tmp
中Tmp
是保存您要查找的数字的一个。因此,哪个部分应该在循环中,哪个部分应该在设置中。正如我所说,我对此非常陌生。当然,对不起。我将编辑带有一些注释的代码。给我一分钟左右的时间。我只尝试复制floop()
函数的功能性。那么如何打印结果?我发布了另一个关于这个主题的问题,你能帮我吗
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
class dummy
{
public:
dummy()
{
const char *testData = "234124!3455addg#5867";
int dataLen = strlen(testData);
mData = new char[dataLen+1];
strcpy(mData, testData);
mTotal = strlen(testData);
mIndex = 0;
}
int available()
{
return mTotal - mIndex;
}
char read()
{
return mData[mIndex++];
}
private:
char *mData;
int mIndex;
int mTotal;
};
char *testFunc()
{
dummy *Serial = new dummy();
/// -------- 8< ------------ cut here until the next pair of scissors. put inside the loop function
/// your code does all of the functionality (reading and buffering) inside a single iteration of loop().
/// Normally, I'd expect a single character to be read each time. I'd expect loop() to be
/// run 16 times before a result was output, since # is the 16th character of the string.
char tmpBuffer[5] = {0};
int bufferIndex = 0;
bool marker1Seen = false;
while (Serial->available() > 0)
{
char received = Serial->read();
if (received == '!')
{
marker1Seen = true;
}
else if (received == '#')
{
return strdup(tmpBuffer);
}
else if (marker1Seen == true && bufferIndex < 4)
{
tmpBuffer[bufferIndex++] = received;
}
}
// shouldn't get here if the input is well-formed
return NULL;
/// -------- 8< ------------ cut here
}
int main()
{
char *result = testFunc();
cout << result;
delete result;
}