从字符串中删除控制字符



我有一个给定的字符串,它包含以下内容(因此以下行存储在string变量中(:

S*⸮
------------------------
K!
NAG                00.10
K"
NMAGICSTAR 2 L    V1.0-1
K#
AUFSTELLORT:  S 00000000
K$
GERAET NR.:   0000000000
KC
ZULASSUNGS NR.:411107770
K)
BAUART:          NAG5A02
K(
ABLAUFDATUM:     2021/04
------------------------

有人能帮我或给我一个简短的提示吗?如何从这个字符串中删除控制代码(因此S*⸮K!((控制代码之前总是有一个小矩形,我不知道为什么要删除它(?所以最后,它是

------------------------
NAG                00.10
NMAGICSTAR 2 L    V1.0-1
AUFSTELLORT:  S 00000000
GERAET NR.:   0000000000
ZULASSUNGS NR.:411107770
BAUART:          NAG5A02
ABLAUFDATUM:     2021/04
------------------------

最后让我引用文档中的一些内容,也许它会有所帮助:

每行最长24个字符,必须以LF[0Ah]结尾

控制代码"ESC‘S’21h LF";表示:XON Startsequence与制造商代码、机器代码和数据集代码

我正试图在ESP32/Arduino IDE(C++(上完成整个任务。

这不是一个anwser。您可以使用以下代码以十六进制形式将字符串打印为整数。每12个字符一个宽分隔,每24个字符一行。这种排列方式使您更容易计数24个字符。

#include <iostream>
void dump_str(const std::string&str)
{
int n;
std::cout << std::hex;
for (int i=0; i<str.size(); i++)
{
n = str[i];
if (i%24==0) std::cout << std::endl;
else if (i%12 == 0 ) std::cout <<"  ";
if (n<16) std::cout << " " << '0' << n;
else  std::cout << " " << n;
}
}
int main ()
{
std::string str ( "somerttestrst2athis is n a ran5dom10gnnTake this for granted. To bo or not to beaatt");
dump_str(str);
}

打印出这个例子:(数字的含义可以在ascii表谷歌搜索中检查。(

73 6f 6d 65 0d 09 74 65 73 74 0d 73  74 02 07 74 68 69 73 20 69 73 20 0a
20 61 20 72 61 6e 05 64 6f 6d 08 67  0a 0a 54 61 6b 65 20 74 68 69 73 20
66 6f 72 20 67 72 61 6e 74 65 64 2e  20 54 6f 20 62 6f 20 6f 72 20 6e 6f
74 20 74 6f 20 62 65 07 07 09 09

将字符串发送到上面的函数dump_str(string(,并将结果表复制到您的帖子中。

以下是如何在和字符处拆分字符串。通过这种方式,您可以分别迭代每一行。

void str_split(const std::string& in,  std::vector<std::string>& out, const std::string& delim=" trn")
{
std::string::size_type firstPos = in.find_first_not_of(delim);
std::string::size_type secondPos = in.find_first_of(delim, firstPos);
out.clear();
if(firstPos != std::string::npos)
out.push_back( in.substr( firstPos, secondPos - firstPos ) );
while( secondPos != std::string::npos )
{
firstPos = in.find_first_not_of(delim, secondPos);
if(firstPos == std::string::npos)
break;
secondPos = in.find_first_of( delim, firstPos );
out.push_back( in.substr( firstPos, secondPos - firstPos ) );
}
}

最新更新