Thread: Converting Integers to Strings

  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
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    Store the result in a char, or cast it to char for printing.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 12:44 PM
  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