How to send integers as text through Serial.write

This is probably old news for the experienced programmers, but i did a lot of searching without finding an answer to my problem. I was writing a software in C# where i planned to readln from serial input something in the lines of START|1|2|3|4|5|END where the integers was read from analog input.

I had problems verifying concated strings, so when i tried something like this, it only sent the integers as bytevalues.

int foo = 1;
int bar = 2;

Serial.write("START|");
Serial.write(foo);
Serial.write("|");
Serial.write("bar");
Serial.write("|END");

Serial.write("\n");

…would be read by C#'s Serial.Readline-method as “START|49|50|END” or something. I could of course just settled for handling that kind of input, but i was somewhat intrigued that i couldnt get it to work.

After trying various approaches, i solved the problem with something along these lines:

String s;
int foo = 1;
int bar = 2;
s = "START|";
s+=foo;
s+="|";
s+=bar;
s+="|END";
//and then iterating by char in the string and sending them as string through serial followed by a newline  like this:
for (int i=0;i<s.length();i++) {
  Serial.write(s[i]); 
}
Serial.write("\n");

I know it’s simple, but this little encounter was my main obstacle in trying to have a C#-program reading 11 analog sensors every 100ms and plotting values into a datagrid and a live updated graph. The START, and END and | was just my way of handling verification of the packets sent (e.g. a pipe-splitted string should countain START first and SEND last, and n pipes.

Hope this can be of use to anyone!

Cheers

Have you considered Serial.print?

int foo = 1;
int bar = 2;

Serial.write("START|");
Serial.print(foo,DEC);
Serial.write("|");
Serial.print(bar,DEC);
Serial.write("|END");

Serial.write("\n");

No i have not tested that. If there's an even more elegant solution, thats great. However, i felt i did a lot of searching for some way to do this, only to end up trying various approaches on my own. I was under the impression that this should be trivial to do, so if this thread directs ppl to an easy solution, although my own in my original post might not be the optimal one, thats what i hoped to achieve. Appreciate your input!

Cheers