ESP32 将String 字符依次存到数组

String payload="";
int byteCount = 0;
for (int i = 0; i < payloa.length(); i++)
{
    char c = payloa.charAt(i);
    if ((c & 0x80) == 0) byteCount += 1;      // 1-byte character
    else if ((c & 0xE0) == 0xC0) byteCount += 2; // 2-byte character
    else if ((c & 0xF0) == 0xE0) byteCount += 3; // 3-byte character
    else if ((c & 0xF8) == 0xF0) byteCount += 4; // 4-byte character
}
Serial.println(byteCount);
if (byteCount > 128)
{
int N = (byteCount / 128)+1; // 计算需要的字符串数量,向上取整
String strings[N]; // 创建一个String数组
// 将输入数据分割成128字节的字符串
for(int i = 0; i < N; i++) {
  for(int j = 0; j < 128 ; j++) {
    int index = 128 * i + j;
    if (index < byteCount) {
      strings[i] += payloa[index];
    } else {
      break;
    }
  }
}
// 打印字符串
for(int i = 0; i < N; i++) {
  MySerial.print(strings[i]);
  Serial.print("O");
  while (1) {
    if (MySerial.available() > 0) {
      // 有数据可读取,退出循环
      String receivedData = MySerial.readString(); // 读取数据
      Serial.print("k");
      if(receivedData="OA"){Serial.print("LAI");delay(10000); break;}
    }
  }
}
}else{
    // If payload is less than 128 bytes, just send it
    MySerial.print(payloa);
}

你可能感兴趣的:(前端)