Concatenation in C++

Thread Starter

Joe24

Joined May 18, 2007
52
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...
 

Mark44

Joined Nov 26, 2007
628
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.

Rich (BB code):
#define size 8
char X[size + 1] = "12345678";
char Y[size + 1] = "8765421";

char both[2*size + 1];

strncat(both, X, size);  // copies size characters of X to both
strncat(both, Y, size);  // copies size characters of Y to both
 

Thread Starter

Joe24

Joined May 18, 2007
52
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??
 

Mark44

Joined Nov 26, 2007
628
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:
Rich (BB code):
short X = 0x1234, Y = 0x4321;
int both = 0x1000 * X + Y;
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
 

RiJoRI

Joined Aug 15, 2007
536
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
 

Mark44

Joined Nov 26, 2007
628
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
z = (x << 16) + y; would be equivalent to multiplying x by 0x1000 as I had in my example.
 

Thread Starter

Joe24

Joined May 18, 2007
52
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..
 
Top