Concatenation in C++

Discussion in 'Programmer's Corner' started by Joe24, Nov 10, 2008.

  1. Joe24

    Thread Starter Active Member

    May 18, 2007
    52
    0
    Hello all,

    I have a problem..I am not a very seasoned C++ programmer, but am forced to write some code for a specific project. One part that I am stuck on is the following:

    If i have two variables, say X=0x12345678 and y=0x87654321

    how do I concactenate these two to form one single
    variable z=0x1234567887654321..

    Any and all the help would be greatly appreciated.

    Thanks...
     
  2. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    Concatenation makes sense only with strings of characters, so you will need to convert the numbers you have into strings.

    Here's how it might be done using the older-style C run-time library function strncat, which concatenates the first n characters of its second argument to its first argument.

    You could do the same thing with the newer style C++ classes such as string, but I haven't kept up on the developments there and tend to lapse into the older style of doing things.

    Code ( (Unknown Language)):
    1.  
    2. #define size 8
    3. char X[size + 1] = "12345678";
    4. char Y[size + 1] = "8765421";
    5.  
    6. char both[2*size + 1];
    7.  
    8. strncat(both, X, size);  // copies size characters of X to both
    9. strncat(both, Y, size);  // copies size characters of Y to both
    10.  
     
  3. Joe24

    Thread Starter Active Member

    May 18, 2007
    52
    0
    Thanks for that reply.

    Ok so lets say I need to store this new concactenated value to a variable of type int or double. Can I declare a variable say:

    double w;

    double = Both;

    Basically I need ONE variable to store the entire value, and not in an array just a single variable: W = 0x1234567887654321;

    Please advise..

    Thanks amil??
     
  4. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    Nope, you can't store the contents of a string in a double or int, but why would you want to?

    The fact that you want to be able to concatenate one string of hex digits with another instead of applying an arithmetic operation such as addition strongly suggests to me that the data type should be string.

    It's possible to have two variables of an integral type (int, long, etc.) that you can essentially concatenate, so maybe that's the direction you want to go. Here's an example:
    Code ( (Unknown Language)):
    1.  
    2. short X = 0x1234, Y = 0x4321;
    3. int both = 0x1000 * X + Y;
    4.  
    Assuming that this code runs in an environment with 2-byte shorts and 4-byte ints, after the second line executes, the variable both will be set to 0x12344321.

    Mark
     
  5. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    How about

    z = (x << 8) + y ?

    Or use a union? It's been a while since I did this, so you'll need to look up the details, but it's something like declare a structure for x and y, call it xy. Then declare a union of z and xy, call it u.

    HTH,
    --Rich
     
  6. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    z = (x << 16) + y; would be equivalent to multiplying x by 0x1000 as I had in my example.
     
  7. Joe24

    Thread Starter Active Member

    May 18, 2007
    52
    0
    Yes Mark, that example you posted with the variable of type shorts is the way I want to go in my code. That is actually a very simple way of achieving what I need in my program. I should try to think simple rather than complicated. I will try and post back if I have any follow up questions. Which I probably will.

    Thanks..
     
Loading...