Help with int to string

HI, I'm a complete putz with C programming but can someone show me how to get this to work. I'm simply trying to get three integers into a string called reading.

  int a0 = 124;
  int a1 = 2315;
  int a2 = 567;
  String reading = "";
  reading = itoa(a0) + itoa(a1) + itoa(a2);

as you can see I'm thinking like a BASIC or Python programmer. but I'm finding C very confusing.

Hello
Take a search engine on your choise and ask the WWW:
Concatenate strings in Arduino

String reading = String(a0) + String(a1) + String(a2);

Probably worth noting that there are 2 types of "strings" in Arduinoland.

One is the "String" (upper case S) class.. as you have declared in your example... it has some very useful methods that are quite easy to use, especially for a beginner. However, if not used carefully it can also cause memory issues, so a lot of people go for the more traditional C based "string" (lower case S) variety.

In C a string is simply a "character array"... char myCharArray[10] for example.

itoa is a C function that works with the latter, converting an integer to a character array.

itoa(myInt, myCharArray, base). The array must be big enough to hold the sign, the number itself, and the terminating HEX 0 character.

There are a number of other C functions that can be used to handle character arrays (strings)... for example strcat() that joins 2 together.

So take your pick...

https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/

or

1 Like

This is the biggest problem with String. How can a beginner, for whom c-strings are allegedly too hard to understand (??), be expected to be careful about String's memory issues? Wouldn't it be easier to learn about c-strings than to learn about dynamic memory allocation and about why it is usually better avoided on a microcontroller?

@dazza000 You are thinking like a user of higher-level languages, that often tend to be dynamically typed. There's nothing wrong with that... as long as you are indeed using such a language. Let us forget about String for a while. In C, a string (lowercase) is just an array of numbers. Each number is interpreted as an ASCII symbol. The string "ABC" actually contains 4 numbers: 0x41, 0x42, 0x43, 0x00. There is nothing in the string itself that contains letters. The last zero, by the way, is the terminating null: it tells the string-handling functions that the string is complete and contains no more characters.

So, what are you trying to do? You want to convert 3 integers into a single string containing the concatenation of the ASCII values of each digit of each number, in decimal.

Note that

124 == 0174 == 0x7C

Choosing decimal is both arbitrary and relevant: same number, different ASCII output.

You can make 3 buffers with itoa() and concatenate them into a single string; you can make it extra-hard and write bespoke code for everything; but why not use sptintf() and do everything in one go, the easy way, and without the infamous String class?

/* First, we make a buffer large enough to hold 3 fat integers
 * and a terminating null.
 */
char reading[20];

/* Second, we populate our string with sprintf() */
sprintf (reading, "%d%d%d", a0, a1, a2);

/* Third (optional), we do something with our new string */
Serial.print("The string is ");
Serial.print(reading);
Serial.print("\n");

If you have negative integers, you will have minus signs mixed in between your digits. You can change the output by changing the format string (i.e. "%d%d%d").

Type

man 3 printf

if you want the whole story.

sprintf() will have you covered for decimal (signed and unsigned), hexadecimal and octal. If you want something special (say, base 5 or base 13), then you have to use itoa() and concatenate buffers.

3 Likes

This is how I would do it, using a char array instead of a String:

int a0 = 124;
int a1 = 2315;
int a2 = 567;
char reading[11];
reading[0] = '0' + (a0 / 100);
reading[1] = '0' + ((a0 / 10) % 10);
reading[2] = '0' + (a0 % 10);
reading[3] = '0' + (a1 / 1000);
reading[4] = '0' + ((a1 / 100) % 10);
reading[5] = '0' + ((a1 / 10) % 10);
reading[6] = '0' + (a1 % 10);
reading[7] = '0' + (a2 / 100);
reading[8] = '0' + ((a2 / 10) % 10);
reading[9] = '0' + (a2 % 10);
reading[10] = 0;

that only works if a0, a1, a2 have 3,4,3 digits respectively... and aren't negative... so not very flexible.

Given that the OP apparently intends to concatenate the numbers (as strings of ASCII digits) without inserting any spaces or punctuation to indicate where one number ends and the next begins, I assume that s/he knows how many digits each number contains.

consider code below producing following output

 a0 124, a1 2315, a2 567
void
setup ()
{
    Serial.begin (9600);

    int a0 = 124;
    int a1 = 2315;
    int a2 = 567;

    char s [40];
    sprintf (s, " a0 %d, a1 %d, a2 %d", a0, a1, a2);
    Serial.println (s);
}

// -----------------------------------------------------------------------------
void
loop ()
{
}