2

I am currently calling _itoa_s and then calling strlen to figure out how many characters were written. Is there a similar method to itoa that either returns the new pointer to the start of the buffer, or returns how many characters were written? I have not been able to find a method in the standard library that provides this functionality.

EDIT:

This is for some performance sensitive code. I am using itoa because it is to my testing the fastest method of doing the string conversion using the standard library.

cigien
  • 57,834
  • 11
  • 73
  • 112
MrMazerman
  • 41
  • 3
  • 3
    Is this a C or a C++ question? Your tags seem a bit muddled. – Adrian Mole Dec 30 '20 at 19:22
  • I'm not aware of any such function, but writing your own implementation of `itoa()` is not difficult, so if it comes down to it you could do that. (although I'd be surprised if the extra CPU cycles used by calling `strlen()` on a short string are even measurable, so it might not be worth the effort) – Jeremy Friesner Jan 10 '21 at 01:37
  • I'm looking for an anwer to the same question and thinking to get a high-performance implemenation it's best to hand-roll as the standard implmentation will usually rely on division modulo the *radix* which implies an `idiv` in assembly, but if this is fixed to 10 (or even better 16!) the compiler will optimise (try it [here](https://godbolt.org/). Some thoughts for implementation of a general itoa are in [this thread](https://stackoverflow.com/questions/3440726/what-is-the-proper-way-of-implementing-a-good-itoa-function) – stevecu Feb 04 '22 at 21:14
  • Also if you know something about the number to be converted, e.g. always positive, fewer than 5 digits etc, you can skip some checks and perhaps even unroll the loop. – stevecu Feb 04 '22 at 21:20

2 Answers2

4

You can use the sprintf() function, which returns the number of characters written to the buffer (not including the nul terminator, BTW):

#include <stdio.h>

int main()
{
    int n;
    char str[64];
    printf("Enter number: ");
    scanf("%d", &n);
    int nc = sprintf(str, "%d", n);
    printf("Characters written = %d\n", nc);
    return 0;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • I aiming to reduce format strings, as this portion of code is very performance sensitive, hence why I am aiming to remove the strlen call. – MrMazerman Dec 30 '20 at 20:11
  • @MrMazerman Try some benchmarking between calls to `sprintf` (with the `%d` format) and calls to `itoa_s`. You may be surprised at the results. – Adrian Mole Dec 30 '20 at 20:13
2

In C++, you can simply convert an int to a string as follows:

std::string str = std::to_string(42);
auto l = str.length(); //2

To get the size easily and circumvent the need for buffers. Although you can get the char* pointer by calling str.data().

Although you might have to update your question with your code if you want more specific answers.

0RR
  • 1,538
  • 10
  • 21
  • This portion of code is highly performance sensitive, so the extra memory allocation is not a fit for my use case unfortunately. – MrMazerman Dec 30 '20 at 20:12