Thread: Convert int to String (no itoa)

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    5

    Convert int to String (no itoa)

    Hello everybody,

    I guess this question has been asked before, but I couldn't find it...
    I need to write the function IntToStr, I can't use itoa, and I'm stuck. Could you please help me?

    thanks

    Maga

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Use sprintf perhaps?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    I think he's supposed to implement his own version.
    Code:
    char str[4]
    int i=213;
    str[0]=i/100+'0';
    i%=100;
    str[1]=i/10+'0';
    i%=10;
    str[2]=i+'0';
    str[3]='\0';
    See if you can find a way to generalize this and use it in yoru IntToStr function.

    edit: Fixed typo. Thanks, vart.
    Last edited by NeonBlack; 03-24-2010 at 06:52 PM.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    char* str[4]
    at least make it of correct type
    Code:
    char str[4];
    and of cause it should be a little longer...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM