5

Hello I'm trying to port some code from Windows to Linux. I have this:

itoa(word,aux,2);

But GCC doesn't recognize itoa. How can I do this conversion to base 2 on a C++ way?

Thanks ;)

Rui
  • 51
  • 1
  • 2
  • no, gcc does not recognize itoa. You could start by naming the types of word,aux,2 – Tom Jul 11 '10 at 23:58
  • See [How would I convert decimal into binary?](http://stackoverflow.com/questions/2801859/how-would-i-convert-decimal-into-binary). It's specifically about formatting an integer as a binary string in C++. Necrolis's solution actually returns a string (rather than printing to cout). – Matthew Flaschen Jul 12 '10 at 00:21

5 Answers5

2

Here´s some help

/* itoa:  convert n to characters in s */
 void itoa(int n, char s[])
 {
     int i, sign;

     if ((sign = n) < 0)  /* record sign */
         n = -n;          /* make n positive */
     i = 0;
     do {       /* generate digits in reverse order */
         s[i++] = n % 10 + '0';   /* get next digit */
     } while ((n /= 10) > 0);     /* delete it */
     if (sign < 0)
         s[i++] = '-';
     s[i] = '\0';
     reverse(s);
 }

You should adapt it to your needs (notice this one has 2 arguments, not 3) Also notice the reverse function is also in the wikipedia link.

Additionally, here are some other cases (but not for base 2)

This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.

A standard-compliant alternative for some cases may be sprintf:

sprintf(str,"%d",value) converts to decimal base.

sprintf(str,"%x",value) converts to hexadecimal base.

sprintf(str,"%o",value) converts to octal base.

Tom
  • 43,810
  • 29
  • 138
  • 169
2

Here is Tom's answer, modified to use the base as your code requires:

void itoa(int n, char s[], std::size_t base = 10) {
    const char digits[] = "0123456789abcdef";
    int i, sign;

    if (base < 2 || base > 16)
        throw std::invalid_argument("invalid base");
    if ((sign = n) < 0)  /* record sign */
        n = -n;      /* make n positive */
    i = 0;
    do {       /* generate digits in reverse order */
        s[i++] = digits[n % base]; /* get next digit */
    } while ((n /= base) > 0);         /* delete it */
    if (sign < 0)
        s[i++] = '-';
    s[i] = '\0';
    reverse(s);
}
Head Geek
  • 38,128
  • 22
  • 77
  • 87
1

There is a page of itoa implementations here

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
1

what about boost::lexical_cast ???

http://www.boost.org/doc/libs/1_43_0/libs/conversion/lexical_cast.htm

Davit Siradeghyan
  • 6,053
  • 6
  • 24
  • 29
  • I love this tool, although be warned that [it isn't the fastest](http://stackoverflow.com/a/1251051/1830736). – George Hilliard May 27 '14 at 19:09
  • 1
    I don't know what was in the past, but now it's faster than about everything, look at this performance comparison table :) http://www.boost.org/doc/libs/1_55_0/doc/html/boost_lexical_cast/performance.html – Davit Siradeghyan Jun 08 '14 at 12:01
0

What about using the power of recursion? :)

int convert_base(int v, int b){ 

    if (v == 0) return 0;   
    else 
        return (v%b+10*convert_base(v/b,b));    
}
Ezequiel
  • 1
  • 1