Thread: convert int to char and concatenate

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    76

    convert int to char and concatenate

    How could I convert an int to char and then concatenate the char variables.

    ok this is what I am thinking but it doesn't work:

    int var1 = 35, var2 = 36;

    var1 = static_cast<char>(var1);
    var2 = static_cast<char>(var2);

    cout << var1 + var2;


    Well my answer should be #$ I am lost here?

    any suggestions?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    630
    Use lexical_cast.

    If you want to convert int to char, you shouldnt cast char to int type.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Your answer is completely implementation-defined, and your program is wrong.

    First, you assume that the machine is using a character encoding that's a superset of ASCII. There's no guarantee that # and $ have the codes 35 and 36, respectively. If you run your program on a different architecture, say, and IBM mainframe, it won't work.

    But let's assume the computer uses ASCII.

    This line:
    var1 = static_cast<char>(var1);
    does nothing. Since var1 is within the range of a char, it really does nothing. If var1 were outside, it would truncate the value. (char is just a small integer with a special interpretation in some contexts.)

    To get char types, you need variables of type char. Types are static in C++: a variable can't change its type.
    Therefore:
    char cvar1 = static_cast<char>(cvar1);

    Finally, the cout.
    var1 + var2 simply adds the two integers.
    If they were chars, it would still simply add the two integers.
    That's because they're integers. Doing anything but adding makes no sense. Note that char can only represent a single character anyway.

    If you want to concatenate with +, you need C++ strings.
    std::string s1 = "#";
    std::string s2 = "$";
    std::string s3 = s1 + s2;

    But that's not necessary here. Since you only want to output them, you can do that.
    cout << cvar1 << cvar2;

    or if you don't want the extra variables, you can cast directly.

    cout << static_cast<char>(var1) << static_cast<char>(var2);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. concatenate int onto char
    By MK27 in forum C Programming
    Replies: 4
    Last Post: 08-06-2008, 09:54 AM
  2. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  3. int and char concatenation question
    By slowcoder in forum C Programming
    Replies: 3
    Last Post: 10-02-2006, 02:14 PM
  4. A bullet-proof fgets wrapper?
    By n7yap in forum C Programming
    Replies: 15
    Last Post: 12-16-2004, 05:19 PM
  5. Binary/Hexadecimal
    By Trauts in forum C++ Programming
    Replies: 24
    Last Post: 09-13-2002, 11:48 PM