Converting Integers to Strings in Arduino

In Arduino programming, you often need to convert integers to strings to display them on the serial monitor or send them over a network. This tutorial will cover various methods for converting integers to strings in Arduino.

Introduction to String Conversion

Arduino provides several ways to convert integers to strings. The most straightforward method is using the String object, which can be initialized with an integer value. However, this approach may not be efficient in terms of memory usage and execution speed.

Using the String Object

To convert an integer to a string using the String object, you can use the following syntax:

int myInt = 123;
String myString = String(myInt);

This method is simple and easy to understand, but it may not be suitable for applications where memory usage needs to be minimized.

Using itoa() Function

Another way to convert an integer to a string is by using the itoa() function from the stdlib.h library. Here’s an example:

int myInt = 123;
char buffer[7];
itoa(myInt, buffer, 10);

The itoa() function converts the integer myInt to a string and stores it in the buffer array.

Using sprintf()

You can also use the sprintf() function to convert an integer to a string. Here’s an example:

int myInt = 123;
char buffer[10];
sprintf(buffer, "%d", myInt);

The sprintf() function formats the integer myInt as a string and stores it in the buffer array.

Using a Custom Function

If you need to optimize your code for memory usage and execution speed, you can create a custom function to convert integers to strings. Here’s an example of a function that uses conditional branches to build the string:

char _int2str[7];

char* int2str(int i) {
  unsigned char L = 1;
  char c;
  boolean m = false;
  char b;  // lower-byte of i

  if (i < 0) {
    _int2str[0] = '-';
    i = -i;
  } else {
    L = 0;
  }

  // ten-thousands
  if (i > 9999) {
    c = i < 20000 ? 1 : i < 30000 ? 2 : 3;
    _int2str[L++] = c + 48;
    i -= c * 10000;
    m = true;
  }

  // thousands
  if (i > 999) {
    c = i < 5000 ? (i < 3000 ? (i < 2000 ? 1 : 2) : (i < 4000 ? 3 : 4)) : i < 8000 ? (i < 6000 ? 5 : i < 7000 ? 6 : 7) : i < 9000 ? 8 : 9;
    _int2str[L++] = c + 48;
    i -= c * 1000;
    m = true;
  } else if (m) {
    _int2str[L++] = '0';
  }

  // hundreds
  if (i > 99) {
    c = i < 500 ? (i < 300 ? (i < 200 ? 1 : 2) : (i < 400 ? 3 : 4)) : i < 800 ? (i < 600 ? 5 : i < 700 ? 6 : 7) : i < 900 ? 8 : 9;
    _int2str[L++] = c + 48;
    i -= c * 100;
    m = true;
  } else if (m) {
    _int2str[L++] = '0';
  }

  // decades
  b = char(i);
  if (b > 9) {
    c = b < 50 ? (b < 30 ? (b < 20 ? 1 : 2) : (b < 40 ? 3 : 4)) : b < 80 ? (i < 60 ? 5 : i < 70 ? 6 : 7) : i < 90 ? 8 : 9;
    _int2str[L++] = c + 48;
    b -= c * 10;
    m = true;
  } else if (m) {
    _int2str[L++] = '0';
  }

  // last digit
  _int2str[L++] = b + 48;

  // null terminator
  _int2str[L] = 0;

  return _int2str;
}

// Usage example:
int i = -12345;
char* s;

void setup() {
  s = int2str(i);
}

void loop() {}

This custom function uses a fixed buffer to store the string and avoids dynamic memory allocation, making it more efficient in terms of memory usage.

Conclusion

Converting integers to strings is an essential task in Arduino programming. You can use the String object, itoa() function, or sprintf() function to achieve this. However, if you need to optimize your code for memory usage and execution speed, creating a custom function using conditional branches may be a better approach.

Leave a Reply

Your email address will not be published. Required fields are marked *