Converting Int to String without conversion in ASCII

I am trying to convert a int variable into a string one, but the String() function is converting it to ASCII. So if I want to convert 255 to "255" I am getting "66" instead. I read that itoa should be able to do that, but when I try itoa(255,buffer,10); it still gives me 66.

In a JSON file I have a bunch of data that looks like this:

{ "0": [intValue, intValue, intValue, intValue, intValue, intValue, intValue, intValue],
"1": [intValue, intValue, intValue, intValue, intValue, intValue, intValue, intValue],
"2": [intValue, intValue, intValue, intValue, intValue, intValue, intValue, intValue],
...
...
...
"3000": [intValue, intValue, intValue, intValue, intValue, intValue, intValue, intValue]
}

I am choosing a random number from 0 to 3000 and trying to convert that to string to be able to extract the corresponding data using doc[].as<JsonArray>()

int random_number = random(3000);
JsonArray current_neighbors = doc[random_number].as<JsonArray>();

This is how my function looks like:

void Neighbors() {
    char current_char;
    int random_number = random(256);
    Serial.println("random_number");
    Serial.println(random(256));
    char current_number[20];
    itoa(random_number,current_number,DECIMAL_BASE);  
    Serial.println("current_number");
    Serial.println(current_number);
    File neighbors_file;
    neighbors_file = SD.open("NBHD.txt"); // Make sure the file name has max 8 characters
    char json_text[neighbors_file.size()];     
    unsigned int string_index = 0;
    StaticJsonDocument<capacity> doc;
    if (neighbors_file) {
    // read from the file until there's nothing else in it:
      while (neighbors_file.available()) {
        current_char = neighbors_file.read();
        json_text[string_index] = current_char;
        string_index++;
        json_text[string_index] = '\0';
      }
    DeserializationError error = deserializeJson(doc, json_text);

    if (error) {
      Serial.print(F("deserializeJson() failed: "));
      Serial.println(error.f_str());
      return;
    }

    JsonArray current_neighbors = doc[current_number].as<JsonArray>();
    Serial.println("Neighbors");
    for(JsonVariant v : current_neighbors) {
      Serial.println(v.as<int>());
     }
    neighbors_file.close();
}
}

And this is what I get:

You do not provide any actual code, so it is not easy to help. Look at this - if it does not work, you are doing something wrong.

Works for me:

void setup() {
  char buffer[4];
  Serial.begin(115200);
  itoa(255,buffer,10);
  Serial.println(buffer);
}

void loop() {
}

Must be a problem in the code you didn't post.

I added my code to the post if that helps. Thank you!

Which board are you using? The line "char json_text[neighbors_file.size()]" does not allow null-termination of the contents. It should also be possible to do a single line read with "file.read(buffer, file.size())".

EDIT: How large is the file you are reading? With 3000 lines and multiple entries on each line It may very well exceed the available memory..

I'm using Teensy 4.1. Right now I am only using a subset of the file so only 255 lines and I updated the capacity based on what ArduinoJson Assist suggested for this file.

Don't you think that the printed value should match the internally used one?
Printing a new random number does not make sense to me.

That's that I meant to do :woman_facepalming:
They are equal now. Thanks :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.