440,091 Members | 1,546 Online
Need help? Post your question and get tips & solutions from a community of 440,091 IT Pros & Developers. It's quick & easy.

# How to concatenate two integer values

 P: n/a 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! Jul 23 '05 #1
30 Replies

 P: n/a priya wrote: 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 . 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. Jul 23 '05 #2

 P: n/a 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 Jul 23 '05 #3

 P: n/a priya wrote: 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. Jul 23 '05 #4

 P: n/a priya wrote: 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? -- Karl Heinz Buchegger kb******@gascad.at Jul 23 '05 #5

 P: n/a Jacques Labuschagne wrote: 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. Jul 23 '05 #6

 P: n/a priya wrote: Hi How to concatenate two integer Values. [code] It would be a great help if the above issues can be resolved. Thanks a lot! 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. :( Jul 23 '05 #7

 P: n/a Jaspreet wrote: 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. Jul 23 '05 #8

 P: n/a "Jacques Labuschagne" skrev i en meddelelse news:42********@clear.net.nz... 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); what is wrong with ss >> n3? Jacques. /Peter Jul 23 '05 #9

 P: n/a 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. :-) Jul 23 '05 #10

 P: n/a priya wrote: 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 Jul 23 '05 #11

 P: n/a "Karl Heinz Buchegger" wrote in message news:42***************@gascad.at... priya wrote: 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 Jul 23 '05 #12

 P: n/a Howard wrote: "Karl Heinz Buchegger" wrote in message news:42***************@gascad.at... priya wrote: 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? 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 kb******@gascad.at Jul 23 '05 #13

 P: n/a priya wrote: 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 Jul 23 '05 #14

 P: n/a Jacques Labuschagne wrote: 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. Jul 23 '05 #15

 P: n/a James Daughtry wrote: 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. Jul 23 '05 #16

 P: n/a priya, If you want concatenate two integers and assign to long type. You can use the below code. #include 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<

 P: n/a Rapscallion wrote: Jacques Labuschagne wrote: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. I don't like knee-jerk optimisation. Stringstreams have never been more than a blip on any of my profiling runs. Jacques. Jul 23 '05 #18

 P: n/a Peter Koch Larsen wrote: "Jacques Labuschagne" skrev i en meddelelse long int n3 = strtol(ss.str().c_str(), NULL, 10); what is wrong with ss >> n3? D'oh! Thanks. Jacques. Jul 23 '05 #19

 P: n/a > 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. Jul 23 '05 #20

 P: n/a James Daughtry wrote: 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? 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. Jul 23 '05 #21

 P: n/a raj wrote: If you want concatenate two integers and assign to long type. You can use the below code. .... sprintf(s1,"%d",a); sprintf(s2,"%d",b); strcat(s1,s2); One sprintf here is enough, isn't it? Jul 23 '05 #22

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

 P: n/a James Daughtry wrote: 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. That's what I propose. But it's not me who insists that stringstream is faster. Jul 23 '05 #24

 P: n/a 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 #include #include #include #include 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. Jul 23 '05 #25

 P: n/a James Daughtry wrote: I honestly don't see what your problem is. I don't have any problem. You have made unfounded performance assertions so far. 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: .... 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. Jul 23 '05 #26

 P: n/a > 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. The trick is, of course, that you define std::stringstream outside the loop. 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". Jul 23 '05 #27

 P: n/a Rapscallion schreef: James Daughtry wrote: 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 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 Jul 23 '05 #28

 P: n/a msalters wrote: 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. 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 Jul 23 '05 #29

 P: n/a Rapscallion schreef: msalters wrote: 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. I see you are an expert. You have only overlooked dynamic memory allocation. Otherwise you are, of course, right. 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 Jul 23 '05 #30

 P: n/a Rapscallion wrote: I see you are an expert. You have only overlooked dynamic memory If you're incapable of using dynamic memory you could always use a std::strstream. Jacques. Jul 23 '05 #31

### This discussion thread is closed

Replies have been disabled for this discussion.