Thread: Convert integer to character

  1. #16
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    It solves for getting a single character only. For instance you have number 62

    the You would call the function as byteTochar(62,1), which would return 6+48=54 or "6"
    Again if byteTochar(62,2) then it would return 2+48=50 or "2"

    Oh I made a simple mistake by the way ,
    In line 10, it should be
    Code:
    anybyte%10
    instead of
    Code:
    10%anybyte
    EDIT: byte data type can be obtained by including windows.h
    Last edited by Swoorup; 11-27-2011 at 10:11 AM.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Swoorup
    It solves for getting a single character only. For instance you have number 62

    the You would call the function as byteTochar(62,1), which would return 6+48=54 or "6"
    Again if byteTochar(62,2) then it would return 2+48=50 or "2"
    In that case, your function is misnamed, and the parameters should not be of type byte.

    As for improvement: implement the function that I proposed.
    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. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why do you simply not use the function manasij7479 have already suggested? Why do you have to make it so hard on yourself?
    Furthermore, why are you using printf?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Because I have to limit the memory as far as possible. Only a byte is to be passed and output of two characters are needed.
    But no worries, I got it working.
    Furthermore the numbers are only 0 to 63.

    Thanks you guys for help

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Swoorup
    Because I have to limit the memory as far as possible. Only a byte is to be passed and output of two characters are needed.
    (...)
    Furthermore the numbers are only 0 to 63.
    In that case, you should do something like this:
    Code:
    /* Converts integer number in range [0, 63] to its numeric decimal string
     * representation, storing the result in a char array without null termination.
     */
    void integerToCharArray(byte number, char* result) {
        result[0] = (number / 10) + '0';
        result[1] = (number % 10) + '0';
    }
    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

  6. #21
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Ah this is much better thank you maam!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. to convert from string to integer
    By vopo in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2008, 06:32 AM
  2. Replies: 5
    Last Post: 06-12-2007, 02:18 PM
  3. convert to integer...
    By compile in forum C Programming
    Replies: 5
    Last Post: 01-22-2007, 08:32 AM
  4. integer and char, convert?
    By virre in forum C++ Programming
    Replies: 1
    Last Post: 01-21-2002, 12:03 AM