Thread: Convert integer to character

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    83

    Convert integer to character

    How can I convert an integer to characters value?

    For instance there is a number from 0 to 64
    Then the
    Integer Character equivalent
    0 "00"
    1 "01"
    2 "02"
    3 "03"
    4 "04"
    5 "05"
    6 "06"
    7 "07"
    10 "10"
    11 "11"

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Read up on stringstreams.
    ..Here is an example:
    Code:
    std::string convert(int n)
    {
        std::ostringstream os;
        os<<n;
        return os.str();
    }

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Basically this.

    int d;
    static_cast<char>(d)

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by whiteflags View Post
    Basically this.

    int d;
    static_cast<char>(d)
    How would that turn 11 into "11" ?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Do you want a string or a character? If you really want a character, my answer is correct. If you really want a string, your answer is correct, manasij.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Ok conjuring up my own findings, Actually I decided that a instead of integer a byte is to be converted to 2 characters.
    Suppose we have 0x01 as a byte then the string equivalent would be 0x3031
    For 63 number, the conversion would be 0x3F then the string equivalent would be 0x3633.

    How can I get that done?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Swoorup
    Actually I decided that a instead of integer a byte is to be converted to 2 characters.
    Suppose we have 0x01 as a byte then the string equivalent would be 0x3031
    For 63 number, the conversion would be 0x3F then the string equivalent would be 0x3633.
    How did you determine the conversion?
    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

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    http://www.dolcevie.com/js/converter.html

    I
    also studied about low level programming.

    EDIT: HELP!!!
    Last edited by Swoorup; 11-27-2011 at 08:36 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sorry, but there is no algorithm on that page (except maybe if you view source). How did you determine the conversion?

    If all you did was to put some text into an online converter and get the result, then you need to figure out what that online converter is doing, then implement an algorithm to do the same thing.
    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

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    No, I did not mean that.
    The online converter just inputs string characters not integers or byte characters.

    I just know about how strings, floating points, long, bytes gets laid out in the memory.

    EDIT:
    Suppose you have two characters "05"
    Then we know that byte equivalent of "0" is 0x30 and for "5" 0x30+0x5=0x35

    So the memory stores the characters this way: 0x30,0x35

    EDIT: I just want to convert a byte to two characters.
    Last edited by Swoorup; 11-27-2011 at 08:45 AM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Swoorup
    Suppose you have two characters "05"
    Then we know that byte equivalent of "0" is 0x30 and for "5" 0x30+0x5=0x35
    You can also say that the 'byte equivalent of "0"' is '0', or assuming ASCII, 48. There is nothing special about using hexadecimal notation, other than it being convenient when we want to deal with bytes.

    Anyway, what is your question now?
    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

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    So how would you convert 10 to "10", when in the memory its laid out as 0x0A, how to make it 0x3031?????

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Swoorup
    So how would you convert 10 to "10", when in the memory its laid out as 0x0A, how to make it 0x3031?????
    Refer to manasij7479's post #2.
    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

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Ok since I could consider the string as a whole to be reliable variable, where I'd just want two characters I made my own function.
    Please if there can be room for improvement please tell me. It works for values from 00 to 99 only though
    Code:
    byte byteTochar(byte anyByte,byte strPos){
      byte character=0;
      
      switch (strPos) 
      {
      case 1:
        if (anyByte>9){character=anyByte/10;}
        break;
      case 2:
        if (anyByte>9){character=10%anyByte;}else{character=anyByte;}
        break;
      default:
        exit(0);//
        break;
      }
        character+=48;
        printf("%d",character);
      return character;
    }

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Let's return to your question:
    Quote Originally Posted by Swoorup
    So how would you convert 10 to "10"
    In other words, you want to convert an integer to a string. Maybe the integer is an int, or a long, or some other integer type, but it is an integer nonetheless. Maybe the string is a std::string, a null terminated string, or some other string type, but it is a string nonetheless.

    Now take a look at your function:
    Code:
    byte byteTochar(byte anyByte,byte strPos)
    I don't know what is byte, but your function clearly does not solve your problem. I would expect something like this:
    Code:
    std::string intToString(int number)
    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. 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