Creating a chr array from an integer array

Hi,

I read char string[] = {"-1200,2000,3300,-4150,5150,6000"}; from an SD card. Then I parse each element and store them into an integer array. I do some calculations with the integers and update my integer array.
Now I need to convert all the elements from my integer array to a char array to save it on the SD card.

What's the best way to do that ?

sprintf.
Or individual prints.
Define "best"

Ok, I've gotten so far:

int nums[3] { -150, -120, -170};
String string[] = {"-110,-123,-177"};
byte n;
byte s = 0;
String myString;
int myInt;

char buffer[5];

void setup() {

  Serial.begin(9600);       

  int sizeString = (sizeof(string));

  for (n = 0; n < 3; n++) {
    myInt = nums[n];
    sprintf(buffer, "%d", myInt); /*convert int to a string and store inside a buffer*/
    myString = String(buffer);

    Serial.print("Integer Converted to String: ");
    Serial.println(myString);   /*Print string value on serial monitor*/

  }

 
}

void loop() {

}

But now I don't know how to replace the old strings with the new strings (numbers) into my string array.

Appreciate your help

there's a difference between a String and a c-string (char array).

could be as simple as this
output:

-150,-120,-170
int nums[3] { -150, -120, -170};

char buffer [80];

void setup()
{
    Serial.begin(9600);

    sprintf (buffer, "%d,%d,%d", nums [0], nums [1], nums [2]);
    Serial.println (buffer);
}

void loop() {
}

Hello

Here is an example t1137640 - Wokwi ESP32, STM32, Arduino Simulator (fixed a small mistake, oops)

1 Like

Thank you guix, appreciate it

Do not use char arrays at all, save the integers as integers

int nums[3] = {-2200, 1234, 4533};
File myFile;

myFile = SD.open("test.dat", FILE_WRITE);
if (myFile) {
  for (n = 0; n < 3; n++) {
   int myInt = nums[n];
   myFile.write(myInt, sizeof(int));
 }
 myFile.close();
 Serial.println("done.");
} 
1 Like

Thank you. That was my original intention but I didn't know how to implement it.

@b707 , How do you read back the integer array from the SD card ?

It does not compile. Error this line:

myFile.write(myInt, sizeof(int));

no matching function for call to 'write(int&, unsigned int)

Please show the link to the SD library you using

#include <SPI.h>
#include <SD.h>

I think your code writes binary numbers, not integers. Without any separator, like maybe a "," , it will be impossible to read it back ?!?

Please change the line this way:

yes, it writes integers as binary data. Any data types - ints. longs, chars.... are represented as binary inside the controller.

You don't need a separator when storing/reading binary data, because the data has fixed size. Therefore, to read 3 ints you need to read 3 times of int data size bytes:

myFile.read((uint8_t*) &myInt, sizeof(int));

Hi,
Here's the code I tried. It compiles and loads, but won't print anything on the serial monitor. Missing something ? Thanks.



#include <SPI.h>
#include <SD.h>

File myFile;

int nums[3] = {-2200, 1234, 4533};
int n;
int myInt;

void setup() {
Serial.begin(9600);

SD.remove("example.dat"); 

myFile = SD.open("example.dat", FILE_WRITE);
if (myFile) {
  for (n = 0; n < 3; n++) {
    myInt = nums[n];
   myFile.write((uint8_t*) &myInt, sizeof(int));
 }
  myFile.close();
 Serial.println("done.");
}

myFile = SD.open("example.dat", FILE_READ);
while (myFile.available()) {
      Serial.println(myFile.read((uint8_t*) &myInt, sizeof(int)));
    }

myFile.close();
}

void loop() {
}

What you tried to print is return byte of the Fire.read() command, but not a myInt value.

Try this:

Sorry, still no data on the serial monitor. I checked the SD card, it's ok.
I tried an example from SD library, works fine.
I looked at the SD card on PC, There's no "example.dat" file on the card.

You did something incorrectly. Double check your code and setup.

This code works for me:

#include <SPI.h>
#include <SD.h>

void setup() {
  
  Serial.begin(115200);
  Serial.print("Initializing SD card...");

  if (!SD.begin(PA8)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
  
  File myFile;

  int nums[3] = { -2200, 1234, 4533};
  int myInt;

  // Writing
  Serial.println();
  Serial.print("Writing to file... ");
  myFile = SD.open("example.dat", FILE_WRITE);
  if (myFile) {
    for (byte n = 0; n < 3; n++) {
      myInt = nums[n];
      myFile.write((uint8_t*) &myInt, sizeof(int));
      Serial.print(myInt);
      Serial.print(" ");
    }
    myFile.close();
    Serial.println(" writing done.");
  }
  Serial.println();


  // Reading
  Serial.print("Reading from file... ");
  myFile = SD.open("example.dat", FILE_READ);
  if (myFile.available()) {
    for (byte n = 0; n < 3; n++) {
      myFile.read((uint8_t*) &myInt, sizeof(int));
      Serial.print(myInt);
      Serial.print(" ");
    }
  }
  Serial.println(" reading done.");
  myFile.close();
}

void loop() {
  // nothing happens after setup
}

output on Serial:

Initializing SD card...initialization done.

Writing to file... -2200 1234 4533  writing done.

Reading from file... -2200 1234 4533  reading done.

1 Like

Great ! It works fine. Thank you very much.
This is the "best" solution, without writing text files, parsing, transforming text to integer, etc. . Kudos.