将动态字符拆分为 N 大小的块 C++
Split dynamic char into N sized chunks C++
我有一个很长的动态字符,我正试图将其分成最多 20 个字符的块。下面的代码是我到目前为止所拥有的。我面临的问题是它似乎在“for loop calculations
”部分有效,但是当我在“Checking results
”中再次读出结果时,它们似乎都设置为最后一次输出第一节.
char* largeWord = "aaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbccccccccccccccccccccddddddddddddddddddddeeeeeeeeeeeeeeeeeeeefffffffffffffffffff";
char* chunks[] = {
"will be overridden"
};
int index = 0;
const int blockSize = 20;
Serial.println("=== for loop calculations ===");
for (int i = 0; i < strlen(largeWord); i += blockSize ) {
char buff[blockSize];
memcpy(buff, &largeWord[i], blockSize);
chunks[index++] = buff;
Serial.print("\"");
Serial.print(buff);
Serial.print("\"");
Serial.println();
}
Serial.println("=== Checking results ===");
for (int i = 0; i < 5; i++ ) {
Serial.print(i);
Serial.print("\"");
Serial.print(chunks[i]);
Serial.print("\"");
Serial.println();
}
这个输出是:
=== for loop calculations ===
"aaaaaaaaaaaaaaaaaaaa⸮!"
"bbbbbbbbbbbbbbbbbbbb⸮!"
"cccccccccccccccccccc⸮!"
"dddddddddddddddddddd⸮!"
"eeeeeeeeeeeeeeeeeeee⸮!"
"fffffffffffffffffff"
=== Checking results ===
0"fffffffffffffffffff"
1"fffffffffffffffffff"
2"fffffffffffffffffff"
3"fffffffffffffffffff"
4"fffffffffffffffffff"
正如您从输出中看到的那样,“for loop calculations
”是它正在计算的内容,也是我所期望的,但我得到的只是“fffff...
”输出。我应该如何解决这个问题?
(另外在旁注中,也出现了“⸮!”,所以我想我在内存分配方面做错了什么?)
更新:
我已尝试更新我的代码并修复它使用以下代码访问越界索引的问题:
char* largeWord = "aaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbccccccccccccccccccccddddddddddddddddddddeeeeeeeeeeeeeeeeeeeefffffffffffffffffff";
char chunks[6][20] = {};
int index = 0;
const int blockSize = 20;
for (int i = 0; i < strlen(largeWord); i += blockSize ) {
memcpy(chunks[index++], &largeWord[i], blockSize);
}
Serial.println("=== Checking results ===");
for (int i = 0; i < 5; i++ ) {
Serial.print(i);
Serial.print("\"");
Serial.print(chunks[i]);
Serial.print("\"");
Serial.println();
}
这似乎越来越接近我想要的结果,因为现在的输出是:
=== Checking results ===
0"aaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbccccccccccccccccccccddddddddddddddddddddeeeeeeeeeeeeeeeeeeeefffffffffffffffffff"
1"bbbbbbbbbbbbbbbbbbbbccccccccccccccccccccddddddddddddddddddddeeeeeeeeeeeeeeeeeeeefffffffffffffffffff"
2"ccccccccccccccccccccddddddddddddddddddddeeeeeeeeeeeeeeeeeeeefffffffffffffffffff"
3"ddddddddddddddddddddeeeeeeeeeeeeeeeeeeeefffffffffffffffffff"
4"eeeeeeeeeeeeeeeeeeeefffffffffffffffffff"
所以 memcpy 似乎并没有像它的本意那样将其限制为 20。我在这里遗漏了什么吗?
我的机器上没有安装 Arduino 来编写代码
我认为您遇到的问题是,当您打印块[i] 时,它一直读取直到到达空终止符 '[=10=]'
。
因为在 C 和 C++ 中,字符串以 null 结尾,或者你可以说它是一个以 null 值结尾的 char 数组,而 C/C++ 中的许多函数都依赖于以 null 结尾的字符串知道什么时候停止阅读,所以你应该在每 20 个字节(字符)之间添加空终止符作为分隔符,所以如果你选择这种方法,你还需要将 blockSize 更改为 21 而不是 20
或使用 substring() Documentation here 使用以下语法:
myString.substring(from, to)
from
将是您的偏移量,并将 to
设置为您要阅读的字符数并注意,如果您选择这种方法,则不必拆分字符串,除非您还有另一个这样做的理由。
我修复了你的代码问题如下
char* largeWord = "aaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbccccccccccccccccccccddddddddddddddddddddeeeeeeeeeeeeeeeeeeeefffffffffffffffffff";
char* chunks[6];
int index = 0;
const int blockSize = 20;
Serial.println("=== for loop calculations ===");
for (int i = 0; i < strlen(largeWord); i += blockSize ) {
char* buff=new char[blockSize+1];
memcpy(buff, &largeWord[i], blockSize);
buff[blockSize] = '[=10=]';
chunks[index++] = buff;
Serial.print("\"");
Serial.print(buff);
Serial.print("\"");
Serial.println();
}
Serial.println("=== Checking results ===");
for (int i = 0; i < 5; i++ ) {
Serial.print(i);
Serial.print("\"");
Serial.print(chunks[i]);
Serial.print("\"");
Serial.println();
}
我有一个很长的动态字符,我正试图将其分成最多 20 个字符的块。下面的代码是我到目前为止所拥有的。我面临的问题是它似乎在“for loop calculations
”部分有效,但是当我在“Checking results
”中再次读出结果时,它们似乎都设置为最后一次输出第一节.
char* largeWord = "aaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbccccccccccccccccccccddddddddddddddddddddeeeeeeeeeeeeeeeeeeeefffffffffffffffffff";
char* chunks[] = {
"will be overridden"
};
int index = 0;
const int blockSize = 20;
Serial.println("=== for loop calculations ===");
for (int i = 0; i < strlen(largeWord); i += blockSize ) {
char buff[blockSize];
memcpy(buff, &largeWord[i], blockSize);
chunks[index++] = buff;
Serial.print("\"");
Serial.print(buff);
Serial.print("\"");
Serial.println();
}
Serial.println("=== Checking results ===");
for (int i = 0; i < 5; i++ ) {
Serial.print(i);
Serial.print("\"");
Serial.print(chunks[i]);
Serial.print("\"");
Serial.println();
}
这个输出是:
=== for loop calculations ===
"aaaaaaaaaaaaaaaaaaaa⸮!"
"bbbbbbbbbbbbbbbbbbbb⸮!"
"cccccccccccccccccccc⸮!"
"dddddddddddddddddddd⸮!"
"eeeeeeeeeeeeeeeeeeee⸮!"
"fffffffffffffffffff"
=== Checking results ===
0"fffffffffffffffffff"
1"fffffffffffffffffff"
2"fffffffffffffffffff"
3"fffffffffffffffffff"
4"fffffffffffffffffff"
正如您从输出中看到的那样,“for loop calculations
”是它正在计算的内容,也是我所期望的,但我得到的只是“fffff...
”输出。我应该如何解决这个问题?
(另外在旁注中,也出现了“⸮!”,所以我想我在内存分配方面做错了什么?)
更新:
我已尝试更新我的代码并修复它使用以下代码访问越界索引的问题:
char* largeWord = "aaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbccccccccccccccccccccddddddddddddddddddddeeeeeeeeeeeeeeeeeeeefffffffffffffffffff";
char chunks[6][20] = {};
int index = 0;
const int blockSize = 20;
for (int i = 0; i < strlen(largeWord); i += blockSize ) {
memcpy(chunks[index++], &largeWord[i], blockSize);
}
Serial.println("=== Checking results ===");
for (int i = 0; i < 5; i++ ) {
Serial.print(i);
Serial.print("\"");
Serial.print(chunks[i]);
Serial.print("\"");
Serial.println();
}
这似乎越来越接近我想要的结果,因为现在的输出是:
=== Checking results ===
0"aaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbccccccccccccccccccccddddddddddddddddddddeeeeeeeeeeeeeeeeeeeefffffffffffffffffff"
1"bbbbbbbbbbbbbbbbbbbbccccccccccccccccccccddddddddddddddddddddeeeeeeeeeeeeeeeeeeeefffffffffffffffffff"
2"ccccccccccccccccccccddddddddddddddddddddeeeeeeeeeeeeeeeeeeeefffffffffffffffffff"
3"ddddddddddddddddddddeeeeeeeeeeeeeeeeeeeefffffffffffffffffff"
4"eeeeeeeeeeeeeeeeeeeefffffffffffffffffff"
所以 memcpy 似乎并没有像它的本意那样将其限制为 20。我在这里遗漏了什么吗?
我的机器上没有安装 Arduino 来编写代码
我认为您遇到的问题是,当您打印块[i] 时,它一直读取直到到达空终止符 '[=10=]'
。
因为在 C 和 C++ 中,字符串以 null 结尾,或者你可以说它是一个以 null 值结尾的 char 数组,而 C/C++ 中的许多函数都依赖于以 null 结尾的字符串知道什么时候停止阅读,所以你应该在每 20 个字节(字符)之间添加空终止符作为分隔符,所以如果你选择这种方法,你还需要将 blockSize 更改为 21 而不是 20
或使用 substring() Documentation here 使用以下语法:
myString.substring(from, to)
from
将是您的偏移量,并将 to
设置为您要阅读的字符数并注意,如果您选择这种方法,则不必拆分字符串,除非您还有另一个这样做的理由。
我修复了你的代码问题如下
char* largeWord = "aaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbccccccccccccccccccccddddddddddddddddddddeeeeeeeeeeeeeeeeeeeefffffffffffffffffff";
char* chunks[6];
int index = 0;
const int blockSize = 20;
Serial.println("=== for loop calculations ===");
for (int i = 0; i < strlen(largeWord); i += blockSize ) {
char* buff=new char[blockSize+1];
memcpy(buff, &largeWord[i], blockSize);
buff[blockSize] = '[=10=]';
chunks[index++] = buff;
Serial.print("\"");
Serial.print(buff);
Serial.print("\"");
Serial.println();
}
Serial.println("=== Checking results ===");
for (int i = 0; i < 5; i++ ) {
Serial.print(i);
Serial.print("\"");
Serial.print(chunks[i]);
Serial.print("\"");
Serial.println();
}