Thread: Converting an Integer into (eventually) an APString.

  1. #1
    Unregistered
    Guest

    Converting an Integer into (eventually) an APString.

    I am in DOS mode, so there is no IntToStr().

    I have to get this int into an apstring. =/

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    does your compiler support itoa() or _itoa() ?

    if it does you can use that to get a char* string then pass that to the apstring constructor.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Here is psuedocode to convert from int to string.

    While the number is > 1
    modulus the number by 10, convert the result to an ANSI char (add 48 I believe), add the char to the string.
    Divide the number by 10

    Reverse the string

    Essentially, you "trap" the ones of the number each time, and divide it by 10 each time, thus sliding down the 1s place.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    Personally, why are you using apstring? apstring is one of the worst string classes you can use...but anyways, this is how to do it:

    int x = 10;
    apstring myString;
    char *cstr = itoa(x, NULL, 10);
    myString = cstr;

    There you go. You must include stdlib.h to be able to use itoa(), that is all.
    My Website

    "Circular logic is good because it is."

  5. #5
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Yeah.. I don't know why the board doesn't just teach the STL rather than use the AP classes.

    Regardless, the apstring class is a subset (and nothing more), of the STL string class, likewise for the apvector (with the possible exception of the member for finding capacity, apvector uses length() while vector uses size(), IIRC.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    other possibilities as itoa() isn't standard are:-
    1) sprintf()
    2)stringstreams.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. fatal error LNK1104
    By DMH in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2005, 03:46 AM