Converting from int to string


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Converting from int to string

  1. #1
    Emma Guest

    Converting from int to string


    Hoping someone can point me in the right direction. I want to convert from
    an int to a string.
    I have been trying to use itoa(int, char *, base) - but when my method exits
    I am getting memory access violation error messages (and then my program
    crashes!).

    Any suggestion?

    Thanks

    Emma

  2. #2
    Danny Kalev Guest

    Re: Converting from int to string

    you're probably using a buffer that is too small to store the result
    string, or worse yet -- a dangling pointer. To avoid all this perils, I
    would recommend using strstream objects instead of itoa(). You can find
    examples and details here:
    http://gethelp.devx.com/techtips/cpp.../10min0401.asp

    Danny

    Emma wrote:
    >
    > Hoping someone can point me in the right direction. I want to convert from
    > an int to a string.
    > I have been trying to use itoa(int, char *, base) - but when my method exits
    > I am getting memory access violation error messages (and then my program
    > crashes!).
    >
    > Any suggestion?
    >
    > Thanks
    >
    > Emma


  3. #3
    Mike Guest

    Re: Converting from int to string


    Emma try the following:
    int i;
    char str[27];

    for( i =0;i<26;i++)
    str[i]=i+65;//65 is the ASCII dec value of the letter A

    str[26]='\0';// Null terminator
    printf("%s",str);
    //should print
    //ABCDEFGHIJKLMNOPQRSTUVWXYZ

    ch=getch();
    return 0;

    mike
    "Emma" <emparker@dingoblue.net.au> wrote:
    >
    >Hoping someone can point me in the right direction. I want to convert from
    >an int to a string.
    >I have been trying to use itoa(int, char *, base) - but when my method exits
    >I am getting memory access violation error messages (and then my program
    >crashes!).
    >
    >Any suggestion?
    >
    >Thanks
    >
    >Emma



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center