how many ways to convert a integer to a string

Discussion in 'C Programming' started by apple.davinci@gmail.com, Mar 9, 2006.

  1. Guest

    as many as possible.
    int i = 333;
    char* s;
    how to convert i to s;
     
    , Mar 9, 2006
    #1
    1. Advertising

  2. wrote:
    > as many as possible.
    > int i = 333;
    > char* s;
    > how to convert i to s;


    Your question is not clear enough (and should have been repreated in
    the body of the post as well). Do you want to use standard functions
    (see `sprintf`), or are you looking for different /algorithmic/ ways?
     
    Vladimir S. Oka, Mar 9, 2006
    #2
    1. Advertising

  3. pete Guest

    wrote:
    >
    > as many as possible.
    > int i = 333;
    > char* s;
    > how to convert i to s;


    #include <stdlib.h>
    #include <limits.h>
    #include <stdio.h>

    s = malloc((size_t)((sizeof(int) * CHAR_BIT - 1) / 3.3) + 3);
    if (s != NULL) {
    sprintf(s, "%d", i);
    }

    --
    pete
     
    pete, Mar 9, 2006
    #3
  4. WaterWalk Guest

    写é“:

    > as many as possible.
    > int i = 333;
    > char* s;
    > how to convert i to s;


    Another way is to get every digit from the integer and then convert the
    difit to it's corresponding char. Just continue to divide the integer
    by 10 until the result is 0. The remainder of each division is the
    leftmost digit of the integer.
     
    WaterWalk, Mar 9, 2006
    #4
  5. CBFalconer Guest

    wrote:
    >
    > as many as possible.
    > int i = 333;
    > char* s;
    > how to convert i to s;


    You could write some code, or you could use the standard library
    that comes with most C systems (all hosted C systems). It's up to
    you. If you write the code you get to define 'convert'. One
    possibility follows:

    void convert(int i) {

    if (i < 0) putchar('-');
    else i = -i;
    while (i++ < 0) putchar('1');
    putchar('\n');
    }

    Note that I took advantage of the ability to define. Some would
    consider this a base 1 representation of i. By using a file I
    avoided the necessity of providing a possibly very large string
    buffer.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
     
    CBFalconer, Mar 9, 2006
    #5
  6. <> wrote in message
    news:...
    > as many as possible.
    > int i = 333;
    > char* s;
    > how to convert i to s;
    >

    I can come up with as many different ways as you would like. Some are not
    very efficient, some are.

    For example, I can convert it to a string in base 2, base 3, ... base 12,
    base 60, whatever.
    I can just use sprintf(), or keep dividing by 10 (for base 10), or take the
    log and do some fancy, stupid manipulation and finally get a string
    representation.

    You will have to be more specific about what you want. Or what your
    professor wants.
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project
     
    Fred Kleinschmidt, Mar 9, 2006
    #6
  7. Micah Cowan Guest

    writes:

    > as many as possible.
    > int i = 333;
    > char* s;
    > how to convert i to s;


    Here's another one (untested, but should work great).

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    int i = 333
    int size;
    int xcode;
    char *s;
    const char *format = "%d";

    /* Get the size we need. */
    size = snprintf(NULL, 0, format, i);
    s = malloc(size);
    if (s == NULL) {
    fputs("Couldn't allocate space for the string. Fancy that!\n",stderr);
    xcode = EXIT_FAILURE;
    } else {
    snprintf(s, size, format, i);
    printf("Here you go: %s\n", s);
    xcode = EXIT_SUCCESS;
    }

    return xcode;
    }
     
    Micah Cowan, Mar 9, 2006
    #7
  8. Old Wolf Guest

    CBFalconer wrote:
    >
    > void convert(int i) {
    >
    > if (i < 0) putchar('-');
    > else i = -i;
    > while (i++ < 0) putchar('1');
    > putchar('\n');
    > }


    Tsk tsk, UB if i == INT_MIN and INT_MIN < -INT_MAX.
     
    Old Wolf, Mar 9, 2006
    #8
  9. Old Wolf Guest

    Old Wolf wrote:
    > Tsk tsk, UB if i == INT_MIN and INT_MIN < -INT_MAX.


    Tsk tsk, Old Wolf is reading-impaired
     
    Old Wolf, Mar 9, 2006
    #9
  10. CBFalconer Guest

    Old Wolf wrote:
    > CBFalconer wrote:
    >>
    >> void convert(int i) {
    >>
    >> if (i < 0) putchar('-');
    >> else i = -i;
    >> while (i++ < 0) putchar('1');
    >> putchar('\n');
    >> }

    >
    > Tsk tsk, UB if i == INT_MIN and INT_MIN < -INT_MAX.


    Look again. That can't happen, which is why I wrote it as I did.
    It is entirely possible that -INT_MIN is larger than INT_MAX, but
    not the reverse.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
     
    CBFalconer, Mar 10, 2006
    #10
  11. Guest

    Fred Kleinschmidt 写é“:

    > <> wrote in message
    > news:...
    > > as many as possible.
    > > int i = 333;
    > > char* s;
    > > how to convert i to s;
    > >

    > I can come up with as many different ways as you would like. Some are not
    > very efficient, some are.
    >
    > For example, I can convert it to a string in base 2, base 3, ... base 12,
    > base 60, whatever.
    > I can just use sprintf(), or keep dividing by 10 (for base 10), or take the
    > log and do some fancy, stupid manipulation and finally get a string
    > representation.
    >
    > You will have to be more specific about what you want. Or what your
    > professor wants.

    it is not my homework.

    > --
    > Fred L. Kleinschmidt
    > Boeing Associate Technical Fellow
    > Technical Architect, Software Reuse Project
     
    , Mar 10, 2006
    #11
  12. wrote:
    > Fred Kleinschmidt 写é“:
    >
    > > <> wrote in message
    > > news:...
    > > > as many as possible.
    > > > int i = 333;
    > > > char* s;
    > > > how to convert i to s;
    > > >


    <snip>

    > > You will have to be more specific about what you want. Or what your
    > > professor wants.

    >
    > it is not my homework.


    Whose is it then? If not at all, then:

    a) why did you not make an effort first?
    b) what practical use there is in knowing this?
    c) say what exactly you want to achieve

    --
    BR, Vladimir
     
    Vladimir S. Oka, Mar 10, 2006
    #12
  13. writes:
    > Fred Kleinschmidt 写é“:

    [...]
    >> You will have to be more specific about what you want. Or what your
    >> professor wants.

    > it is not my homework.


    Then, again, you will have to be more specific about what you want.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 10, 2006
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hank
    Replies:
    5
    Views:
    476
    Bengt Richter
    Nov 18, 2003
  2. les ander
    Replies:
    4
    Views:
    893
    wes weston
    Oct 5, 2004
  3. Garg
    Replies:
    1
    Views:
    12,483
    Dejan Lazic
    Aug 4, 2006
  4. Skybuck Flying
    Replies:
    62
    Views:
    2,031
    Rudy Velthuis
    Jun 29, 2010
  5. Randy Kramer
    Replies:
    12
    Views:
    378
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page