How to concatenate two integer values

Discussion in 'C++' started by priya, May 24, 2005.

  1. priya

    priya Guest

    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!
     
    priya, May 24, 2005
    #1
    1. Advertisements

  2. 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?
    If your textbook told you to do it this way then you need a new
    textbook. Use #include <iostream>.

    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.
     
    Jacques Labuschagne, May 24, 2005
    #2
    1. Advertisements

  3. priya

    priya Guest

    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
     
    priya, May 24, 2005
    #3
  4. 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.
     
    Jacques Labuschagne, May 24, 2005
    #4
  5. 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, May 24, 2005
    #5
  6. 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.
     
    Jacques Labuschagne, May 24, 2005
    #6
  7. priya

    Jaspreet Guest

     
    Jaspreet, May 24, 2005
    #7
  8. It's not in C99, and I don't recall seeing it in C++98. Luckily C++ has
    std::stringstream. :)

    Jacques.
     
    Jacques Labuschagne, May 24, 2005
    #8
  9. "Jacques Labuschagne" <> skrev i en meddelelse
    what is wrong with ss >> n3?
    /Peter
     
    Peter Koch Larsen, May 24, 2005
    #9
  10. 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. :)
     
    James Daughtry, May 24, 2005
    #10
  11. 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
     
    Victor Bazarov, May 24, 2005
    #11
  12. priya

    Howard Guest

    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
     
    Howard, May 24, 2005
    #12
  13. 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, May 24, 2005
    #13
  14. 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
     
    Victor Bazarov, May 24, 2005
    #14
  15. priya

    Rapscallion Guest

    stringstream means unnecessary allocation of dynamic memory.

    R.C.
     
    Rapscallion, May 24, 2005
    #15
  16. priya

    Rapscallion Guest

    Really? I guess you have a bug in your tests :)
    Not for me.
     
    Rapscallion, May 24, 2005
    #16
  17. priya

    raj Guest

    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
     
    raj, May 25, 2005
    #17
  18. I don't like knee-jerk optimisation. Stringstreams have never been more
    than a blip on any of my profiling runs.

    Jacques.
     
    Jacques Labuschagne, May 25, 2005
    #18
  19. D'oh! Thanks.

    Jacques.
     
    Jacques Labuschagne, May 25, 2005
    #19
  20. Really?
    Really!
    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?
    *cough* That's a quality of programmer issue. ;-)

    That was a joke, just in case you decide to get all insulted.
     
    James Daughtry, May 25, 2005
    #20
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.