Pages: [1]   Go Down
Author Topic: String + integer  - Sorry for this  (Read 7034 times)
0 Members and 1 Guest are viewing this topic.
Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

-Hello!

I am sorry for this question, but I simply cannot find a solution.
Is there a simple way to mimic this java code?
Code:
int myVar = 2;
System.out.println("My variable is: "+myVar);

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


Yet again, sorry.
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);  
     Serial.print(buffer);
 

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: ");
Serial.println(myVar);


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, “.”);

« Last Edit: January 18, 2008, 04:09:37 am by mem » Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

//SOLVED

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

//:build string+int
string[3] = i;
« Last Edit: January 18, 2008, 05:15:08 am by AlphaBeta » Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can always just do:

Serial.print("my variable is:");
Serial.println(var);
Logged

0
Offline Offline
God Member
*****
Karma: 0
Posts: 507
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

he's right, thats what i do....
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

California
Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's another vote for

Code:
Serial.print("My variable is: ");
Serial.println(myVar);

It's hard to get easier than that.

Regards,
David
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

There is a perfectly resonable reason smiley-grin 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) {
          printByte(*s++);
        }
        printByte(0);
}

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.  :smiley
« Last Edit: January 29, 2008, 03:42:29 am by AlphaBeta » Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 177
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Pages: [1]   Go Up
Jump to: