连接字符串时发生崩溃和错误

连接字符串时发生崩溃和错误

本文介绍了Arduino:连接字符串时发生崩溃和错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将 AES-256 加密的输出连接到一个字符串(以将此字符串与从 Android 手机发送的加密字符串进行比较).

I try to concatenate the output of AES-256 encryption to a string (to compare this string against the encrypted String sent from an Android phone).

基本上,连接似乎有效,但在几次运行错误(非可读字符,字符串变短而不是变长)或崩溃后发生.它是可重现的,重启后在完全相同的点崩溃.

Basically, the concatination seems to work, but after a few runs errors (non readable characters, string getting shorter instead of longer) or crashes occur. It is reproducible, crashes at the exact same point after restart.

我提取了一些演示问题的 Arduino 代码行.它执行以下操作:

I extracted some lines of Arduino code that demonstrate the problem. It does the following:

  1. 创建一个随机数并将其写入数组(有效)
  2. AES- 编码这个数组(有效)
  3. 构建每个数组索引的十六进制表示(有效)
  4. 将索引连接到一个字符串(崩溃)
#include <SPI.h>
#include "aes256.h"  //include this lib

uint8_t key[] = {9,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,
                 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8 }; //the encryption key

aes256_context ctxt; //context needed for aes library


void setup() {
  Serial.begin(9600);
}


void loop() {

  uint8_t data[] = {
       0x53, 0x73, 0x64, 0x66, 0x61, 0x73, 0x64, 0x66,
       0x61, 0x73, 0x64, 0x66, 0x61, 0x73, 0x64, 0x65, }; //the message to be encoded

  long InitialRandom = random(2147483647); //0 to highest possible long
  String RandomString = "" ;
  RandomString+=InitialRandom; //random number to String
  Serial.println(RandomString); //for debugging

  //update data array: Random String into data array
  for (int x=0; x<RandomString.length(); x++){
       data[x] = RandomString[x];
  }

  //this encrypts data array, which changes
  aes256_init(&ctxt, key); //initialize the lib
  aes256_encrypt_ecb(&ctxt, data); //do the encryption
  aes256_done(&ctxt);

  //Here the problem starts.............................................
  String encrypted=""; //the string I need finally

  for (int x=0; x<sizeof(data); x++){ //data array is 16 in size
        int a = data[x];
        String b = String (a, HEX);
        if(b.length()==1) b="0"+b;  //if result is e.g "a" it should be "0a"
        encrypted.concat(b);  //this line causes the problem!!!
        //Serial.println(b); //works perfect if above line commented out
        Serial.println(encrypted); //see the string geting longer until problems occur
  }
  //Here the problem ends.............................................

        Serial.println(); //go for next round, until crashing
}

我搜索了论坛,尝试了不同的连接方式(+ 运算符、strcat).都有类似的效果.我读到 String 库有一个错误,将 Arduino IDE 更新为 1.0.

I have searched the forums, tried different ways to concatenate (+ operator, strcat). All had similar effects. I read that the String library had a bug, updated Arduino IDE to 1.0.

这让我忙了好几天,非常感谢任何帮助,

This has kept me busy for days, any help is highly appreciated,

非常感谢!

推荐答案

您可能内存不足,因为 Arduino 只有少量内存.

You are probably running out of memory as Arduino only has a small amount.

检查循环期间您有多少空闲.

Check how much memory you have free during your loop.

罪魁祸首可能是String的实现(见Arduino WString.cpp) 正在使用 realloc(),并且您的内存可能正在使用一或两个字节的字符串(每个都有 16 字节的堆标头成本)进行大量碎片整理.

The culprit may be that the implementation of String (see Arduino WString.cpp) is using realloc(), and your memory is probably being heavily defregmented with one or two byte strings (each of which has a 16 byte heap header cost).

您可以通过预先分配一个 String Reserve() 函数来预先分配缓冲区,从而更有效地重写上述内容.或者使用本机 C++ 字符数组重写它.

You could re-write the above more efficiently by pre-allocating a String reserve() functions to pre-allocate the buffer. Or re-write it using native C++ char arrays.

这篇关于Arduino:连接字符串时发生崩溃和错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 04:58