Go Down

Topic: String + integer  - Sorry for this (Read 24262 times) previous topic - next topic



I am sorry for this question, but I simply cannot find a solution.
Is there a simple way to mimic this java code?
Code: [Select]

int myVar = 2;
System.out.println("My variable is: "+myVar);

Serial.println("My variable is: "......?);

Yet again, sorry.


Jan 18, 2008, 10:07 am Last Edit: Jan 18, 2008, 10:09 am by mem Reason: 1
The traditional way to format strings in C is sprintf. An example below:

#include <stdio.h>

#define MAX_OUT_CHARS 16  //max nbr of characters to be sent on any one serial command

char   buffer[MAX_OUT_CHARS + 1];  //buffer used to format a line (+1 is for trailing 0)

    sprintf(buffer,"My variable is: %d",myVar);  

But there are lots of reasons to avoid it. It links in a huge amount of code (almost 2k as I recall), its not very intuitive to use, and if you overflow the buffer it creates elusive bugs.

I usually do the following:

Serial.print("My variable is: ");

In my current app, I added the following function to format data for my specific requirement.

void formatPrint( char *leftStr, int MyVar1, char *sepStr, int MyVar2, char *rightStr){
     Serial.print (leftStr);
     Serial.print (MyVar1);
     Serial.print (sepStr);
     Serial.print (MyVar2);
     Serial.println (rightStr);

Usage: formatPrint("My var 1 is: ", 100, ", and var 2 is: ", 200, ".");


Jan 18, 2008, 10:48 am Last Edit: Jan 18, 2008, 11:15 am by AlphaBeta Reason: 1

char string[5] = "val";
int i=50;

//:build string+int
string[3] = i;


That only works if your string never changes in length and the integer value is between 0 and 9, is that what you wanted?


That only works if your string never changes in length and the integer value is between 0 and 9, is that what you wanted?

No, not what I wanted, but it was what I needed, for now, anyway.

Any ideas?


You can always just do:

Serial.print("my variable is:");




Any ideas?
any reason why one of the three suggestions in reply#1 are not suitable?


Here's another vote for

Code: [Select]
Serial.print("My variable is: ");

It's hard to get easier than that.



Jan 29, 2008, 09:35 am Last Edit: Jan 29, 2008, 09:42 am by AlphaBeta Reason: 1
There is a perfectly resonable reason :D for why sending my String and variable in to different method calls won't work.

I wrote the reason in #2 but i see I deleted it. Sorry for that. And I also see that I used Serial.print() as an axample.
I thought that it was easier to get a 'problem solved' when using a method we're all familiar with.

I do not use the serial class.
This is my function:

void sendToSerProxy(char *s) {
     while (*s) {

This is why I need the  string and var inside the same char array.

Of course I know about doing it in separate method calls.  ::)


You're probably best off doing something like this then:

Serial.print("My variable is: ");
Serial.print(0, BYTE);

agent smith

it's easy to send different data formats to the serial port, just send them one by one.
But it's not feasible if you want to put everything to a string then send to a LCD module.
I found if you want to append text to an existing string you have to use strcat(string,value)
but how can i convert an integer to a string before appending it?

I tried the itoa function but it seems the arduino freezes with this instruction, even if it gets compiled. And I don't know anything about C++

In VB all of this would require this thing...

temperature = temperature + str$(value)

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131