Converting Integers to Strings

This is a discussion on Converting Integers to Strings within the C++ Programming forums, part of the General Programming Boards category; HI, part of a project i'm working on requires me to convert integers into characters. So the number 1 would ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    Converting Integers to Strings

    HI, part of a project i'm working on requires me to convert integers into characters. So the number 1 would become '1'. We're not allowed to use any pre-built functions such as atoi or itoa or sprintf. We have to manually create a function. I was thinking you could find the ascii value, but i dont know how you could display the character from the ascii value

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    If the number in question is in the range [0,9], then adding '0' will suffice. If the number is in a larger range, perhaps you want to convert it to a numeric string instead (unless you intend to represent the number as a single "digit" in a larger base, e.g., hexadecimal).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    yea, adding '0' gives the ascii value, but i want the character of the number to be saved

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    Store the result in a char, or cast it to char for printing.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    cast it to char?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    Yes, but since you are supposed to "convert integers into characters", it probably makes sense to cast it to char and store it in a char instead of just casting an int to char for output.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting strings to chars
    By Suchy in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2007, 04:17 AM
  2. Replies: 9
    Last Post: 03-17-2006, 11:44 AM
  3. Converting String to integers
    By Mister C in forum C Programming
    Replies: 21
    Last Post: 06-01-2005, 03:13 PM
  4. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM
  5. Converting Integers to Binary
    By cprog in forum C Programming
    Replies: 19
    Last Post: 10-03-2002, 08:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21