Arduino:连接字符串时崩溃和错误 [英] Arduino: Crashes and errors when concatenating Strings

查看:146
本文介绍了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的实现(请参见

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 ++ char数组重新编写.

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:连接字符串时崩溃和错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆