Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ Integer to String

Name: Anonymous 2008-06-07 5:44

Hi /prog/.

I am right now working on my C++ project for my programming class and I started wondering. What method's do you use to convert integer to string? There is so many of them, and most require at least 4 additional code lines. Which method do you prefer?

Name: Anonymous 2008-06-08 2:56

>>42
#define INTEGERTOSTRING(i)\
  do {\
    __typeof__(i) _i = (i), negative = _i < 0 ? 1 : 0;\
    const char digits = "0123456789";\
    char *str = malloc((size_t)log10(_i) + 2 + negative) + (int)log10(_i) + 1 + negative;\
    *str-- = 0;\
    for(; _i; _i /= 10) *str-- = digits[_i % 10];\
    if(negative) *str-- = '-';\
    ++str;\
  } while (0)

Fixed.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List