Getting

Hi. I'm trying to get an int out of a charArray, but no matter what I do, the I get the ascii number representation of the value. When I print the value to the serial monitor, it shows up properly as the int, but when I try to display it, it shows up as ascii. I have no idea why this is happening. Any ideas? I assume it's a simple conversion thing I'm doing wrong.

char vals[16];

void loop() {

  timeClient.update();
  String value = timeClient.getFormattedTime();
  Serial.println(value);
  int val_len = value.length() + 1;
  value.toCharArray(vals, val_len);

      clearLEDs();
      pickDigit(1);
      pickNumber(vals[7]); //should be an int, but shows up as acsii 
      delay(1);  
      pickDigit(2);
      pickNumber(4);
      delay(1);
      pickDigit(3);
      pickNumber(5);
      delay(1);  
      pickDigit(4);
      pickNumber(6);
      delay(1);
      Serial.println(vals[7]); //prints as the int for some reason

}

Thanks!

Can you post the definition of pickNumber() and pickDigit()?

The simplest method of converting ASCII '0'-'9' to the integers 0-9 is to subtract '0'.

  digit = asciiDigit - '0';

Sorry - the reason I didn't post those functions is that they're just simple sitches that shouldn't effect the outcome of the string.

void pickDigit(int x){
  digitalWrite(19, HIGH); // dig 1
  digitalWrite(25, HIGH); // dig 2
  digitalWrite(26, HIGH); // dig 3
  digitalWrite(12, HIGH); // dig 4

  switch(x){
    case 1: digitalWrite(19, LOW); break;
    case 2: digitalWrite(25, LOW); break;
    case 3: digitalWrite(26, LOW); break;
    case 4: digitalWrite(12, LOW); break;    
  }
}

void pickNumber(int t){
   switch(t){
     case 1: one(); break;
     case 2: two(); break;
     case 3: three(); break;
     case 4: four(); break;
     case 5: five(); break;
     case 6: six(); break;
     case 7: seven(); break;
     case 8: eight(); break;
     case 9: nine(); break;
     default: zero(); break;
   } 
}

The subtract '0' method worked though! Thank you!!!

Post the complete program.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

...R

  int val_len = value.length() + 1;
  value.toCharArray(vals, val_len);

RTFM. The second argument is NOT the length of the String object, with or without the terminating NULL.