Like Tree1Likes

Convert integer to character

This is a discussion on Convert integer to character within the C++ Programming forums, part of the General Programming Boards category; How can I convert an integer to characters value? For instance there is a number from 0 to 64 Then ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Read up on stringstreams.
    ..Here is an example:
    Code:
    std::string convert(int n)
    {
        std::ostringstream os;
        os<<n;
        return os.str();
    }
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Basically this.

    int d;
    static_cast<char>(d)

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by whiteflags View Post
    Basically this.

    int d;
    static_cast<char>(d)
    How would that turn 11 into "11" ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    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
    21,451
    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?
    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

  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 07:36 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    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.
    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

  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 07:45 AM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    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?
    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

  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
    21,451
    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.
    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

  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
    21,451
    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)
    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

Page 1 of 2 12 LastLast
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, 05: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, 07:32 AM
  4. Convert a character to integer (ex '1' to 1)
    By VanJay011379 in forum C++ Programming
    Replies: 11
    Last Post: 03-18-2003, 12:14 AM
  5. integer and char, convert?
    By virre in forum C++ Programming
    Replies: 1
    Last Post: 01-20-2002, 11:03 PM

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