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: