How to concatenate two integer values

P

priya

Hi
How to concatenate two integer Values.


Example Program :


#include "Port.h"
#include "BinaryConversion.h"
# include "iostream.h"
Port p;
long binary(long);

void BinaryConversion::Fire()
{
long number,r;
number=p.GetToken_DecimalNo();
printf("Inside user code %d",number);
r=binary(number);
p.Send(r);
}


long binary(long number)
{
long remainder;
long re;
while(number >1)
{
remainder = number%2;
number = number / 2;
re = re & remainder; //concatenate these two integer values
}

return(re);

}

It would be a great help if the above issues can be resolved. Thanks a
lot!
 
J

Jacques Labuschagne

priya said:
Hi
How to concatenate two integer Values.

What do you mean by concatenation? Do you want to pack two numbers into
the same variable, with one in the high bits and one in the low bits?
Example Program :


#include "Port.h"
#include "BinaryConversion.h"
# include "iostream.h"

If your textbook told you to do it this way then you need a new
textbook. Use #include <iostream>.

long binary(long number)
{
long remainder;
long re;
while(number >1)
{
remainder = number%2;
number = number / 2;
re = re & remainder; //concatenate these two integer values
}

return(re);

}

Why don't you tell us what you want binary() to do? It's hard to tell
from your code. My earlier guess about packing two numbers into the same
variable must be wrong, because you only have one number as input.

J.
 
P

priya

Thanks for your reply

binary() method is for finding the binary value of given decimal
number.

I meant to say :
String concatenation :
string s="hi";
string s1="hello";
string s3=s1+s2;
Result :hihello

Similarly i want to concatenate two integer :
int n=10;
int n1=90;
The result i need : 1090
 
J

Jacques Labuschagne

priya said:
Thanks for your reply

binary() method is for finding the binary value of given decimal
number.

I meant to say :
String concatenation :
string s="hi";
string s1="hello";
string s3=s1+s2;
Result :hihello

Similarly i want to concatenate two integer :
int n=10;
int n1=90;
The result i need : 1090

Well, the easy way is to use strings.
int n1 = 10, n2 = 90;
stringstream ss;
ss << n1 << n2;
long int n3 = strtol(ss.str().c_str(), NULL, 10);

If you have to do it the hard way (e.g. for a homework assignment) then
you need to figure out how many digits there are in n2, e.g. by taking
log10(n2) and then store something like (10*log10(n2)*n1 + n2).
So for your original example you want something that works out to
(10*2*n1 + n2) where n1=10 and n2=90.

Jacques.
 
K

Karl Heinz Buchegger

priya said:
Thanks for your reply

binary() method is for finding the binary value of given decimal
number.

I meant to say :
String concatenation :
string s="hi";
string s1="hello";
string s3=s1+s2;
Result :hihello

Similarly i want to concatenate two integer :
int n=10;
int n1=90;
The result i need : 1090

How about

10 * 100 + 90

or with variables

n * 100 + nl

Now the question for you: Why did I multiply with 100?
 
J

Jacques Labuschagne

Jacques said:
If you have to do it the hard way (e.g. for a homework assignment) then
you need to figure out how many digits there are in n2, e.g. by taking
log10(n2) and then store something like (10*log10(n2)*n1 + n2).
So for your original example you want something that works out to
(10*2*n1 + n2) where n1=10 and n2=90.

Oops... 10 to the power of 2, not 10 times 2. That's how you shift your
first number on by one digit, multiplying it by 10.

Jacques.
 
J

Jaspreet

priya said:
Hi
How to concatenate two integer Values.

Code:
It would be a great help if the above issues can be resolved. Thanks a
lot![/QUOTE]

What seems to be the issue ?

An alternative could be to:
1. Get 2 integers input by the user.
2. Use itoa() from stdlib.h to conevrt them to strings
3. Concatenate the two strings using strcat.

Not sure if itoa() is a part of the standard though. :(
 
J

Jacques Labuschagne

Jaspreet said:
Not sure if itoa() is a part of the standard though. :(

It's not in C99, and I don't recall seeing it in C++98. Luckily C++ has
std::stringstream. :)

Jacques.
 
J

James Daughtry

sprintf fakes itoa pretty well, and it's in all of the standards. But
std::stringstream is easier to use and slightly faster than sprintf and
strtol or sscanf in my tests. Of course, that's a quality of
implementation issue, but std::stringstrea is still easier to use. :)
 
V

Victor Bazarov

priya said:
How to concatenate two integer Values.


Example Program :


#include "Port.h"
#include "BinaryConversion.h"
# include "iostream.h"
Port p;
long binary(long);

void BinaryConversion::Fire()
{
long number,r;
number=p.GetToken_DecimalNo();
printf("Inside user code %d",number);
r=binary(number);
p.Send(r);
}


long binary(long number)
{
long remainder;
long re;
while(number >1)
{
remainder = number%2;
number = number / 2;
re = re & remainder; //concatenate these two integer values
}

return(re);

}

It would be a great help if the above issues can be resolved. Thanks a
lot!

What's the issue? Please read the FAQ before posting again. Pay special
attention to section 5. http://www.parashift.com/c++-faq-lite/

V
 
H

Howard

Karl Heinz Buchegger said:
How about

10 * 100 + 90

or with variables

n * 100 + nl

Now the question for you: Why did I multiply with 100?

What if the numbers were 123 and 2048?

If I undesrstand the "concatenation" concept the OP wanted, that should come
out as "1232048", but your idea would produce 14348. (Obviously, the
original question could have been worded better. Poorly written
requirements produce poorly written programs!)

-Howard
 
K

Karl Heinz Buchegger

Howard said:
What if the numbers were 123 and 2048?

Then the 100 needs to be replaced with something else.
Thats what I wanted the OP to think about when I asked him
where the 100 came from. Eventually he would have figured out
that there is a relationship between the 100 used for multiplication
and the 90 used for catanation.

Maybe I worded it badly.
But I still think that this is 90% of all the fun in programming:
Boldly finding patterns and relationships where noone else has
found them before :) Oh, and getting told is not that satisfying
as finding them by yourself.
 
V

Victor Bazarov

priya said:
Thanks for your reply

binary() method is for finding the binary value of given decimal
number.

I meant to say :
String concatenation :
string s="hi";
string s1="hello";
string s3=s1+s2;
Result :hihello

Similarly i want to concatenate two integer :
int n=10;
int n1=90;
The result i need : 1090

Is there a C++ _language_ problem you're experiencing? I can only see
a generic algorithm problem. Try posting to a relevant newsgroup (I
suggest 'comp.programming'). If you already have the algorithm and
have trouble translating it to C++, show us the pseudo-code (and your
first attempt, if any) and we can help you.

A hint I can give is to use strings as the medium for concatenation.

V
 
R

Rapscallion

Jacques said:
Well, the easy way is to use strings.
int n1 = 10, n2 = 90;
stringstream ss;
ss << n1 << n2;

stringstream means unnecessary allocation of dynamic memory.

R.C.
 
R

Rapscallion

James said:
sprintf fakes itoa pretty well, and it's in all of the standards. But
std::stringstream is easier to use and slightly faster than sprintf and
strtol or sscanf in my tests.

Really? I guess you have a bug in your tests :)
Of course, that's a quality of
implementation issue,
Truly!

but std::stringstrea is still easier to use. :)

Not for me.
 
R

raj

priya,

If you want concatenate two integers and assign to long type. You can
use the below code.

#include <iostream>
using namespace std;

int main ()
{
int a=30000; int b=30100;
char s1[32], char s2[16];
unsigned long r;

sprintf(s1,"%d",a);
sprintf(s2,"%d",b);
strcat(s1,s2);
r = atol(s1);
cout<<r<<endl;
return 0;
}

Thanks,
Rajendran
 
J

Jacques Labuschagne

Rapscallion said:
stringstream means unnecessary allocation of dynamic memory.

I don't like knee-jerk optimisation. Stringstreams have never been more
than a blip on any of my profiling runs.

Jacques.
 
J

James Daughtry

Really?
Really!
I guess you have a bug in your tests :)
Well, it's entirely possible that I don't know what I'm doing. Perhaps
you could provide a test that you've proven correct and says otherwise?
Not for me.
*cough* That's a quality of programmer issue. ;-)

That was a joke, just in case you decide to get all insulted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top