pulkitjain's blog

By pulkitjain, history, 5 years ago, In English

Hi All,

I am trying to find the fastest way to convert integer directly to char* (C-string) without converting it to std::string first. I have already tried using std::to_string() function but then also i need to convert it into C-string using c_str() function. Basically an alternative for itoa in c++ for version 11.

Please feel free to give your views regarding this.

Thanks and Regards
Pulkit Jain

  • Vote: I like it
  • -23
  • Vote: I do not like it

»
5 years ago, # |
  Vote: I like it +11 Vote: I do not like it

How about std::to_chars in C++17?

Also, why do you need to convert to char* instead of using std::strings? It seems unnecessary unless you need to squeeze every bit of performance in your code.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it -11 Vote: I do not like it

    Yes actually, I want to save some nanoseconds. Will to_chars work fine with C++11

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by pulkitjain (previous revision, new revision, compare).

»
5 years ago, # |
  Vote: I like it +3 Vote: I do not like it

You can use sprintf

char s[10];
int i = 123;
int n = sprintf(s, "%d", i);