Chars, Strings, Concatenation and Confused

Hello... I'm really confused with the Char and String things in Arduino - If I can just describe briefly what I'm trying to do.

Ii'm using the Adafruit WaveHC library and trying to play a WAV.

Now, to save RAM I've named all my WAVs with 5 digits e.g 12001.

So if I do playFileNew (12103); I want it to play 12103.WAV. So I would assume it would just be converting the integer to a string and adding on ".WAV"

Now the original code looks more like this

e.g

playFile ("song.wav")

void playFile(char *name) {
  if (!file.open(root, name)) {
    Serial.print(F("\n  playfile:   Couldn't open file: ")); 
    Serial.println(name);
    return;
  }
}

So the question is how to I convert the incoming "12001" into "12001.WAV" to be opened in the playFile ?

I would have thought it would be something like:

Change the playFileNew to accept integers to -- > void playFile(long lName, boolean playComplete) {}

note: I had to use long as for some reason it would not pass 12003 as an integer.

soundFile = convert to String (lName) + ".WAV";

But then this does not work in -- > if (!file.open(root, soundFile)) {

Does this make sense?

Thanks in advance

Hello,

The easiest way is to use sprintf:

char fileName[10]; // enough space for "XXXXX.WAV\0"
sprintf( fileName, "%d.WAV", soundID);

guix:

char fileName[10]; // enough space for "XXXXX.WAV\0"

sprintf( fileName, "%d.WAV", soundID);

This, of course, assumes that soundID is a numeric variable (eg int)

Hello... I'm really confused with the Char and String things in Arduino

Do yourself a favor. Forget that the stupid String class even exists.

Thanks the sprintf worked brilliantly !