Connecting Tech Pros Worldwide Help | Site Map

How to concatenate two integer values

priya
Guest
 
Posts: n/a
#1: Jul 23 '05
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!

Jacques Labuschagne
Guest
 
Posts: n/a
#2: Jul 23 '05

re: How to concatenate two integer values


priya wrote:[color=blue]
> Hi
> How to concatenate two integer Values.[/color]

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?
[color=blue]
>
>
> Example Program :
>
>
> #include "Port.h"
> #include "BinaryConversion.h"
> # include "iostream.h"[/color]

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

<snip>
[color=blue]
> 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);
>
> }[/color]

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.
priya
Guest
 
Posts: n/a
#3: Jul 23 '05

re: How to concatenate two integer values


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

Jacques Labuschagne
Guest
 
Posts: n/a
#4: Jul 23 '05

re: How to concatenate two integer values


priya wrote:[color=blue]
> 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
>[/color]

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.

Karl Heinz Buchegger
Guest
 
Posts: n/a
#5: Jul 23 '05

re: How to concatenate two integer values


priya wrote:[color=blue]
>
> 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[/color]

How about

10 * 100 + 90

or with variables

n * 100 + nl

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

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Jacques Labuschagne
Guest
 
Posts: n/a
#6: Jul 23 '05

re: How to concatenate two integer values


Jacques Labuschagne wrote:[color=blue]
> 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.[/color]

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.
Jaspreet
Guest
 
Posts: n/a
#7: Jul 23 '05

re: How to concatenate two integer values



priya wrote:[color=blue]
> Hi
> How to concatenate two integer Values.
>
>[/color]
[code][color=blue]
> It would be a great help if the above issues can be resolved. Thanks[/color]
a[color=blue]
> lot![/color]

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. :(

Jacques Labuschagne
Guest
 
Posts: n/a
#8: Jul 23 '05

re: How to concatenate two integer values


Jaspreet wrote:[color=blue]
> Not sure if itoa() is a part of the standard though. :(
>[/color]

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

Jacques.
Peter Koch Larsen
Guest
 
Posts: n/a
#9: Jul 23 '05

re: How to concatenate two integer values



"Jacques Labuschagne" <jacques@clawshrimp.com> skrev i en meddelelse
news:4292dad6$1@clear.net.nz...
<snip>[color=blue]
> 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);[/color]

what is wrong with ss >> n3?
[color=blue]
>[/color]
<snip>[color=blue]
> Jacques.
>[/color]

/Peter


James Daughtry
Guest
 
Posts: n/a
#10: Jul 23 '05

re: How to concatenate two integer values


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. :-)

Victor Bazarov
Guest
 
Posts: n/a
#11: Jul 23 '05

re: How to concatenate two integer values


priya wrote:[color=blue]
> 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![/color]

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
Howard
Guest
 
Posts: n/a
#12: Jul 23 '05

re: How to concatenate two integer values



"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
news:4292DB02.A8A2CC42@gascad.at...[color=blue]
> priya wrote:[color=green]
>>[/color]
>
> How about
>
> 10 * 100 + 90
>
> or with variables
>
> n * 100 + nl
>
> Now the question for you: Why did I multiply with 100?
>[/color]

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


Karl Heinz Buchegger
Guest
 
Posts: n/a
#13: Jul 23 '05

re: How to concatenate two integer values


Howard wrote:[color=blue]
>
> "Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
> news:4292DB02.A8A2CC42@gascad.at...[color=green]
> > priya wrote:[color=darkred]
> >>[/color]
> >
> > How about
> >
> > 10 * 100 + 90
> >
> > or with variables
> >
> > n * 100 + nl
> >
> > Now the question for you: Why did I multiply with 100?
> >[/color]
>
> What if the numbers were 123 and 2048?[/color]

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.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Victor Bazarov
Guest
 
Posts: n/a
#14: Jul 23 '05

re: How to concatenate two integer values


priya wrote:[color=blue]
> 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
>[/color]

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
Rapscallion
Guest
 
Posts: n/a
#15: Jul 23 '05

re: How to concatenate two integer values


Jacques Labuschagne wrote:[color=blue]
> Well, the easy way is to use strings.
> int n1 = 10, n2 = 90;
> stringstream ss;
> ss << n1 << n2;[/color]

stringstream means unnecessary allocation of dynamic memory.

R.C.

Rapscallion
Guest
 
Posts: n/a
#16: Jul 23 '05

re: How to concatenate two integer values


James Daughtry wrote:[color=blue]
> 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.[/color]

Really? I guess you have a bug in your tests :-)
[color=blue]
> Of course, that's a quality of
> implementation issue,[/color]

Truly!
[color=blue]
> but std::stringstrea is still easier to use. :-)[/color]

Not for me.

raj
Guest
 
Posts: n/a
#17: Jul 23 '05

re: How to concatenate two integer values


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

Jacques Labuschagne
Guest
 
Posts: n/a
#18: Jul 23 '05

re: How to concatenate two integer values


Rapscallion wrote:[color=blue]
> Jacques Labuschagne wrote:
>[color=green]
>>Well, the easy way is to use strings.
>> int n1 = 10, n2 = 90;
>> stringstream ss;
>> ss << n1 << n2;[/color]
>
>
> stringstream means unnecessary allocation of dynamic memory.
>[/color]

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

Jacques.
Jacques Labuschagne
Guest
 
Posts: n/a
#19: Jul 23 '05

re: How to concatenate two integer values


Peter Koch Larsen wrote:[color=blue]
> "Jacques Labuschagne" <jacques@clawshrimp.com> skrev i en meddelelse[color=green]
>> long int n3 = strtol(ss.str().c_str(), NULL, 10);[/color]
>
>
> what is wrong with ss >> n3?[/color]

D'oh! Thanks.

Jacques.
James Daughtry
Guest
 
Posts: n/a
#20: Jul 23 '05

re: How to concatenate two integer values


> Really?
Really!
[color=blue]
> I guess you have a bug in your tests :-)[/color]
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?
[color=blue]
> Not for me.[/color]
*cough* That's a quality of programmer issue. ;-)

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

Rapscallion
Guest
 
Posts: n/a
#21: Jul 23 '05

re: How to concatenate two integer values


James Daughtry wrote:[color=blue]
> 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?[/color]

Use the original problem:

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
}

Compare sprintf + scanf and stringstream implementations.

Rapscallion
Guest
 
Posts: n/a
#22: Jul 23 '05

re: How to concatenate two integer values


raj wrote:[color=blue]
> If you want concatenate two integers and assign to long type. You can
> use the below code.[/color]
....[color=blue]
> sprintf(s1,"%d",a);
> sprintf(s2,"%d",b);
> strcat(s1,s2);[/color]

One sprintf here is enough, isn't it?

James Daughtry
Guest
 
Posts: n/a
#23: Jul 23 '05

re: How to concatenate two integer values


I was talking about a direct comparison between sprintf and strtol (or
sscanf), and stringstream, not a comparison of those two with a low
level solution.

Rapscallion
Guest
 
Posts: n/a
#24: Jul 23 '05

re: How to concatenate two integer values


James Daughtry wrote:[color=blue]
> I was talking about a direct comparison between sprintf and strtol (or
> sscanf), and stringstream, not a comparison of those two with a low
> level solution.[/color]

That's what I propose. But it's not me who insists that stringstream is
faster.

James Daughtry
Guest
 
Posts: n/a
#25: Jul 23 '05

re: How to concatenate two integer values


I honestly don't see what your problem is. I ran a test with
stringstream and the equivalent solution using sprintf and strtol. My
results on that implementation showed stringstream to be slightly
faster. Here's the test. I don't claim it to be anything but a simple
and naive test, but it is consistent:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <boost/timer.hpp>

int main()
{
int a = 10, b = 90;
char buffer[5];
std::stringstream ss;
boost::timer t;

for (int i = 0; i < 1000000; i++) {
int c;
sprintf(buffer, "%d%d", a, b);
c = (int)strtol(buffer, 0, 0);
}

std::cout << "sprintf/strtol: " << t.elapsed() << '\n';
t.restart();

for (int i = 0; i < 1000000; i++) {
int c;
ss << a << b;
ss >> c;
}

std::cout << "stringstream: " << t.elapsed() << '\n';
}

The results for one run of this test are (with all optimizations turned
off):

sprintf/strtol: 1.203
stringstream: 0.953

I never insisted that stringstream was always faster. In fact, I made
it very clear that the results would be implementation-dependent.
Knowing that my code could have been better, I asked for a test that
you wrote to so that I could run it and admit ignorance if the results
favored sprintf/strtol. Though you still haven't provided anything but
empty claims that I'm wrong (implying all cases) while I have
consistent proof that I'm not.

Rapscallion
Guest
 
Posts: n/a
#26: Jul 23 '05

re: How to concatenate two integer values


James Daughtry wrote:[color=blue]
> I honestly don't see what your problem is.[/color]

I don't have any problem. You have made unfounded performance
assertions so far.
[color=blue]
> I ran a test with
> stringstream and the equivalent solution using sprintf and strtol. My
> results on that implementation showed stringstream to be slightly
> faster. Here's the test. I don't claim it to be anything but a simple
> and naive test, but it is consistent:[/color]

....

The trick is, of course, that you define std::stringstream outside the
loop. BTW, I never trust performance measurements that I haven't forged
myself.

James Daughtry
Guest
 
Posts: n/a
#27: Jul 23 '05

re: How to concatenate two integer values


> You have made unfounded performance assertions so far.
I have code with results and I've posted said code. How is that
unfounded?

As it is, I've admitted multiple times that my test is only legitimate
on the implementation that I ran it. You've admitted that performance
is strictly implementation-dependent. So the only issue should be that
my code is wrong somehow. Please, give me details so that I can learn
from my mistakes.
[color=blue]
> The trick is, of course, that you define std::stringstream outside the loop.[/color]
Seeing as how I'm comparing the conversion with the conversion and not
the conversion with construction and destruction of an object as well
as the conversion, defining the object in my loop would be stacking the
deck by forcing more operations per iteration into the stringstream
loop. That's not an objective test, and if you think it is then that
explains why you're busting my balls about making "unfounded
performance assertions".

msalters
Guest
 
Posts: n/a
#28: Jul 23 '05

re: How to concatenate two integer values




Rapscallion schreef:[color=blue]
> James Daughtry wrote:[color=green]
> > 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.[/color]
>
> Really? I guess you have a bug in your tests :-)[/color]

Of course not. stringstream is an istream and an ostream. They know at
compile time which parser/formatter they need. scanf/printf have to
parse
the format specfier, then select the appropriate parser/formatter, and
then do what istream::operator>> or ostream::operator<< was doing.
Clearly
these extra steps take some time.

HTH,
Michiel Salters

Rapscallion
Guest
 
Posts: n/a
#29: Jul 23 '05

re: How to concatenate two integer values


msalters wrote:[color=blue]
> Of course not. stringstream is an istream and an ostream. They know at
> compile time which parser/formatter they need. scanf/printf have to
> parse
> the format specfier, then select the appropriate parser/formatter, and
> then do what istream::operator>> or ostream::operator<< was doing.
> Clearly these extra steps take some time.[/color]

I see you are an expert. You have only overlooked dynamic memory
allocation. Otherwise you are, of course, right. People less skilled
than you may be interested in the article series: "Efficient Integer To
String Conversions" by Matthew Wilson:
http://synesis.com.au/articles.html

msalters
Guest
 
Posts: n/a
#30: Jul 23 '05

re: How to concatenate two integer values




Rapscallion schreef:[color=blue]
> msalters wrote:[color=green]
> > Of course not. stringstream is an istream and an ostream. They know at
> > compile time which parser/formatter they need. scanf/printf have to
> > parse
> > the format specfier, then select the appropriate parser/formatter, and
> > then do what istream::operator>> or ostream::operator<< was doing.
> > Clearly these extra steps take some time.[/color]
>
> I see you are an expert. You have only overlooked dynamic memory
> allocation. Otherwise you are, of course, right.[/color]

Dynamic memory allocation? In istringstream? That's an implementation
detail, just like in scanf. Either one *could* use dynamic memory
during the sting to integer conversion, but doesn't have to.

In ostringstream? Clearly, the output has to go somewhere. Even sprintf
needs a destination buffer. Now, if you didn't reserve memory up front
ostringstream will indeed allocate dynamic memory. sprintf will simply
overwrite whatever your char* points to. If you did allocate sufficient
memory, neither sprintf nor ostringstream will reserve memory.

HTH,
Michiel Salters

Jacques Labuschagne
Guest
 
Posts: n/a
#31: Jul 23 '05

re: How to concatenate two integer values


Rapscallion wrote:[color=blue]
> I see you are an expert. You have only overlooked dynamic memory[/color]

If you're incapable of using dynamic memory you could always use a
std::strstream.

Jacques.
Closed Thread