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. Advertising

  2. 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 <iostream>.

    <snip>

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

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

    Jaspreet Guest

    priya wrote:
    > Hi
    > How to concatenate two integer Values.
    >
    >

    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. :(
     
    Jaspreet, May 24, 2005
    #7
  8. 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.
     
    Jacques Labuschagne, May 24, 2005
    #8
  9. "Jacques Labuschagne" <> skrev i en meddelelse
    news:4292dad6$...
    <snip>
    > 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?

    >

    <snip>
    > Jacques.
    >


    /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. 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
     
    Victor Bazarov, May 24, 2005
    #11
  12. priya

    Howard Guest

    "Karl Heinz Buchegger" <> wrote in message
    news:...
    > 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
     
    Howard, May 24, 2005
    #12
  13. Howard wrote:
    >
    > "Karl Heinz Buchegger" <> wrote in message
    > news:...
    > > 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
     
    Karl Heinz Buchegger, May 24, 2005
    #13
  14. 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
     
    Victor Bazarov, May 24, 2005
    #14
  15. priya

    Rapscallion Guest

    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.
     
    Rapscallion, May 24, 2005
    #15
  16. priya

    Rapscallion Guest

    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.
     
    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. 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.
     
    Jacques Labuschagne, May 25, 2005
    #18
  19. 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.
     
    Jacques Labuschagne, May 25, 2005
    #19
  20. > 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.
     
    James Daughtry, May 25, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. roxorsoxor2345
    Replies:
    4
    Views:
    483
    Nate Barney
    Oct 6, 2006
  2. MattB
    Replies:
    5
    Views:
    849
    MattB
    Jun 13, 2007
  3. TJHerman

    Concatenate field values

    TJHerman, Jun 13, 2006, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    145
    TJHerman
    Jun 13, 2006
  4. GIMME
    Replies:
    1
    Views:
    174
    Dr John Stockton
    Apr 9, 2004
  5. Carlos

    Concatenate/De-Concatenate

    Carlos, Oct 12, 2012, in forum: VHDL
    Replies:
    10
    Views:
    892
Loading...

Share This Page