int to char and vice versa

This is a discussion on int to char and vice versa within the C++ Programming forums, part of the General Programming Boards category; Yes, I know I'm a very inexperienced programmer but this is a very simple problem that probably has a very ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    7

    int to char and vice versa

    Yes, I know I'm a very inexperienced programmer but this is a very simple problem that probably has a very simple solution. I'd appreciate anyone's help!

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int number1 = 123;
        char string1 = char(number1);
        
        cout << string1 << endl;    // outputs '2293612'
    
    ///////////////////////////////////////
        
        char string2[4] = "123";
        int number2 = int(string2);
        
        cout << string2 << endl;    // outputs '{' because this is the ASCII character "123"
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    In each case I would like the output to be "123." I realize that typecasting won't work. Also, I know I could make an elaborate switch function to get around this but it seems like there must be an easier way. If there is an easy way to do this, could somebody please show me? Thank you very much.

    --The struggling C++ student (whose teacher knows nothing)

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    You can't cast a string like that. Use atoi()

    Code:
    string2[4] = "123";
    int number2 = atoi(string2);
    
    cout << number << '\n' << number + 1 << endl;
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    7
    Wow, thanks a lot. I had never heard of atoi(). What do I do for the other scenario?

    Code:
    int number1 = 123;
    char string1 = char(number1);
        
    cout << string1 << endl;    // outputs '2293612'
    I should be clear that I intend for the output to be "123"
    Last edited by mekaj; 12-11-2005 at 06:25 PM.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    itoa() works a bit differently.

    Code:
    int main(int argc, char *argv[])
    { 
    char string1[4] = "123";
    char string2[4];
    int number2 = atoi(string1);
    itoa(number2,string2,10);  // 10 being for decimal, 2 is binary, 16 is hex
    
    cout << number2 << '\n' << number2 + 1 << '\n' << string2 << endl;
    
    cin.get();
        return EXIT_SUCCESS;
    ...and for future reference.

    atoi() = Alpha to Integer
    itoa() = Integer to Alpha

    Both of which is found in <cstdlib>
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    7
    I can't thank you enough! Finally I won't feel so limited

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    
    int main()
    {
    	union convert1
    	{
    		int number1;
    		char string1;
    	};
    	
    	union convert2
    	{
    		int number2;
    		char string2[4];
    	};
    	
    	convert1 test1;
    	test1.number1 = 123;
    	cout << test1.string1 << endl;
    	
    
    ///////////////////////////////////////
        
    	convert2 test2;
    	for(int i=0;i<3;i++)
    		test2.string2[i] = i;
    	test2.string2[3] = '\0';
    	cout << test2.string2 << endl;
    	
        
    
        return 0;
    }

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Stringstreams are nice. You could give those a try. I'm not sure you really need to use unions, though. They're nice, but <sstream> is something you should look into.
    Sent from my iPadŽ

  9. #9
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by jlf029
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    
    int main()
    {
    	union convert1
    	{
    		int number1;
    		char string1;
    	};
    	
    	union convert2
    	{
    		int number2;
    		char string2[4];
    	};
    	
    	
    }

    sorry, but that's awful. Use stringstreams and if you have a performance problem (streams can be slow), use itoa/atoi.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    heh, i was just being stupid.
    Why not just static_cast the variable, you dont need to include any headers.
    Code:
    int number = 65;
    cout << static_cast<char>(number) << endl;

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    The whole point of this guys post is that didn't work.

    Did you try compiling the code you just posted? Doesn't quite give you "65" does it.
    Sent from my iPadŽ

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    my code?
    Converting number to char. It compiles fine

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    It's really like you didn't read the post at all...

    He wants the integer 65 to convert to the string "65" and he wants the string "65" to produce the integer 65.

    He doesn't want to cast a number and produce it's ascii value.
    Sent from my iPadŽ

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    The itoa function is not standard and may not work on all compilers. Also, the error reporting for atoi is to simply return 0, so you cannot tell if the input was bad or if it was really just "0". String streams are standard and they provide more sophisticated error handling abilities.

    Another note- instead of using char, you should be using the C++ string class for strings. They make dealing with strings easier and less error prone. If your teacher doesn't want you to use them for the class for some reason, then you should still consider learning them on your own.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char vs int - program crash!!
    By Goldrak in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2006, 08:17 PM
  2. tell me about a 'char'
    By Brain Cell in forum C Programming
    Replies: 4
    Last Post: 02-21-2005, 08:05 AM
  3. Binary to ascii and vice versa
    By thenrkst in forum C++ Programming
    Replies: 13
    Last Post: 03-30-2003, 12:17 AM
  4. %i or %d - where is the difference?
    By Sargnagel in forum C Programming
    Replies: 3
    Last Post: 11-12-2002, 02:21 PM
  5. c++ question about char
    By Mazer in forum C++ Programming
    Replies: 14
    Last Post: 07-15-2002, 07:12 PM

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