String bayt qatoriga qanday aylantiriladi

Kodni o'zimga olaman, uning maqsadi quyidagicha: Set @ 1234567890123456 @ 1234567890123456 @ 1234567890123456 @ 1234567890123456 va uni to'rtta bay baytli bayt user1 [16], user2 [16], pass1 [16], pass2 [16] ga aylantiramiz. Kodi:

String inString = "";//COM port incoming data buffer

void setup() {
 //put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Start");
}

void loop() {
 //put your main code here, to run repeatedly:
 //Receive data from com port
  while (Serial.available() > 0) {
    int inChar = Serial.read();


    if (inChar != '\n') {
      inString += (char)inChar;
    } else {
     //New line
      String Action = inString.substring(0, 3);
      if (Action == "Set") {
        SetCard(inString);
      } else if (Action == "Get") {
        Serial.println("1");
      } else {
        Serial.println(Action);
      }
      inString = "";
    }
  }
}

void SetCard(String Data) {
  Serial.println(Data);
 //Data [email protected]@[email protected]@pass2
 //[email protected]@[email protected]@1234567890123456
  byte user1[16], user2[16], pass1[16], pass2[16];

  String user1str = inString.substring(4, 20);
  String user2str = inString.substring(21, 37);
  String pass1str = inString.substring(38, 54);
  String pass2str = inString.substring(55, 71);

  Serial.println("Strings");
  Serial.println(user1str);
  Serial.println(user2str);
  Serial.println(pass1str);
  Serial.println(pass2str);

  Serial.println("Arrays");
  user1str.getBytes(user1, 16);
  user2str.getBytes(user2, 16);
  pass1str.getBytes(pass1, 16);
  pass2str.getBytes(pass2, 16);


  writeByteArray(user1, 16);
  writeByteArray(user2, 16);
  writeByteArray(pass1, 16);
  writeByteArray(pass2, 16);
}

void writeByteArray(byte array[], int arrlength)
{
  for (int j = 0 ; j < arrlength ; j++) //print the block contents
  {
    Serial.write (array[j]);//Serial.write() transmits the ASCII numbers as human readable characters to serial monitor
  }
  Serial.println("");
}

Ushbu kodni ishga tushirganimda va magistralni MAQOMOTI orqali yuborganimda, men chiqishni kutaman:

Start
[email protected]@[email protected]@1234567890123456
Strings
1234567890123456
1234567890123456
1234567890123456
1234567890123456
Arrays
1234567890123456
1234567890123456
1234567890123456
1234567890123456

Va yana, qabul qilaman

Start
[email protected]@[email protected][email protected]
Strings
1234567890123456
1234567890123456
1234567890123456
1234567890123456
Arrays
123456789012345
123456789012345
123456789012345
123456789012345

Why? And how do I receive desired output? getBytes docs

0

1 javoblar

1
qo'shib qo'ydi
Rahmat. U ishladi. Shunday qilib, qiymati user1 [17] qatorini user1 [16] ) ga qanday qilib bera olaman?
qo'shib qo'ydi muallif Richard Rast, manba
Yoki bufer majmuasini yaratishim kerak [17] va keyin u erda birinchi 16 elementni qatorga [16] yozish kerakmi?
qo'shib qo'ydi muallif Richard Rast, manba
Buni o'ylab ko'raylik, faqat uchun (int j = 0; j va 16 element bilan ishlaydi
qo'shib qo'ydi muallif Richard Rast, manba