String problem [solved]

Hi,

I’m calling this function from loop:

void key_device_f(byte id_device) 
{
    uint8_t* hash;
    String s = String(id_device);
    
//    for(int i=0; i<4; i++) {
//      for(int j=16; j<48; j++) {
//        s += String(de_podaci[j]); 
//      }
//    }
//   Sha1.init();
//   Sha1.print(s);
//   hash = Sha1.result(); 
//   
//   Serial.print("\nHash: ");
//   printHash(hash);
}

as you can see, I commented on purpose everything else…I do not get any errors but Serial port freezes each time on the very beginning, as I figured out problem starts with defining String s variable, if I comment that everything is ok.

on the other hand, I have similar sketch that runs well:

#include <sha1.h>

void printHash(uint8_t* hash) {
  int i;
  for (i=0; i<20; i++) {
    Serial.print("0123456789abcdef"[hash[i]>>4]);
    Serial.print("0123456789abcdef"[hash[i]&0xf]);
  }
  Serial.println();
}


void setup() {
  
  byte id=1;
  byte podaci[]={11, 2, 3, 4, 5};
  String s = String(id);
  for(int i=0; i<5; i++)
    s += String(podaci[i]);
  
  uint8_t* hash;
  
  Serial.begin(9600);

  // SHA test
  Serial.println("TEST\n\nInput:");
  Serial.print(s);
  Serial.print("\n\nResult: ");
  
  Sha1.init();
  Sha1.print(s);
  hash = Sha1.result();
  
  Serial.print("\n\n");
  for (int i=0; i<20; i++) {
    Serial.print(" ");
    Serial.print(hash[i]);
  }
  printHash(hash); 
}
void loop() {
  
}

any ideas? please :frowning:

THank you!

any ideas?

Your first code won't even compile and link. No loop() and no setup(). When you post code that will, then perhaps we can help you.

String s = String(id_device);

A call to the constructor to create the String on the right. A call to the copy constructor to create the String on the left. Neither call is required.

char stg[4];
sprintf(stg, "%d", id_device);

PaulS: char stg[4]; sprintf(stf, "%d", id_device);

(In sprintf, it should be: stg to match your array name)

(In sprintf, it should be: stg to match your array name)

Some fat fingered idiot typed that while I wasn't looking. Yeah. That's my story and I'm sticking to it.

Oh, and I fixed my post.

PaulS:
Your first code won’t even compile and link. No loop() and no setup(). When you post code that will, then perhaps we can help you.

full code is in attach

A call to the constructor to create the String on the right. A call to the copy constructor to create the String on the left. Neither call is required.

char stg[4];

sprintf(stg, “%d”, id_device);

got that from example here: http://arduino.cc/en/Reference/StringConstructor

Tried with this example, same result, Serial port freezes :frowning:

code.ino (14.2 KB)

got that from example here

Figures. Crappy documentation to go with a crappy class.

byte podaci[550]; //niz u koji se spremaju o?itavanja senzora
byte konv_vektor[9]={0, 0, 0, 1, 1, 1, 0, 0, 0};
byte rezultat[558];
byte ko_podaci[100];//o?itani podaci, manchester kodirani
byte de_podaci[50];//dekodirani podaci

These 5 arrays are using 1,267 bytes, of the 2048 available in a 328-based Arduino.

I think that you are running out of memory, unless you are using a Mega.

I'm not running of memory :)

I declared to check that out new array of 500 bytes and it's working well

I'm getting improvements, should be fixed soon...thanks anyway

Mods can close this one