string operations

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • j_depp_99@yahoo.com

    string operations

    My program reads in hex values from a file and then performs
    arithmetic operations on them. All works well except when I use large
    hex values and my answers are incorrect. For example 10000000000000-1
    should give
    FFFFFFFFFFFFF but it does not. I considered converting to decimal to
    solve my problem but I'm not sue how to convert my string variable
    into a decimal value.
    Here's a snippet of my code:
    I first convert my two strings into hex, then perform the subtraction,
    but when I check the value of num7, it does not display
    10000000000000. The other operations also involve large hexadecimal
    values. Is there perhaps a limit on the length C++, can handle?

    <code\>
    istringstream(o p5) >std::hex >num7;
    istringstream(o p6) >std::hex >num8;
    cout << op5 << "-" << op6 << "=";
    num9= num7-num8;
    cout.flags(ios: :hex);
    cout << uppercase << hex << num9 << endl;
    </code>

    Thanks
  • Victor Bazarov

    #2
    Re: string operations

    j_depp_99@yahoo .com wrote:
    My program reads in hex values from a file and then performs
    arithmetic operations on them. All works well except when I use large
    hex values and my answers are incorrect. For example 10000000000000-1
    should give
    FFFFFFFFFFFFF but it does not. I considered converting to decimal to
    solve my problem but I'm not sue how to convert my string variable
    into a decimal value.
    Here's a snippet of my code:
    I first convert my two strings into hex, then perform the subtraction,
    but when I check the value of num7, it does not display
    10000000000000. The other operations also involve large hexadecimal
    values. Is there perhaps a limit on the length C++, can handle?
    I believe the comma is misplaced (or just exraneous) in your question.
    C++ can handle as much as you can program it to handle.
    >
    <code\>
    istringstream(o p5) >std::hex >num7;
    istringstream(o p6) >std::hex >num8;
    cout << op5 << "-" << op6 << "=";
    num9= num7-num8;
    cout.flags(ios: :hex);
    cout << uppercase << hex << num9 << endl;
    </code>
    What's the type of 'num7' and 'num8'? Are they large enough to contain
    the external representations you trow at them?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • j_depp_99@yahoo.com

      #3
      Re: string operations

      On Nov 21, 3:29 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
      j_depp...@yahoo .com wrote:
      My program reads in hex values from a file and then performs
      arithmetic operations on them. All works well except when I use large
      hex values and my answers are incorrect. For example 10000000000000-1
      should give
      FFFFFFFFFFFFF but it does not. I considered converting to decimal to
      solve my problem but I'm not sue how to convert my string variable
      into a decimal value.
      Here's a snippet of my code:
      I first convert my two strings into hex, then perform the subtraction,
      but when I check the value of num7, it does not display
      10000000000000. The other operations also involve large hexadecimal
      values. Is there perhaps a limit on the length C++, can handle?
      >
      I believe the comma is misplaced (or just exraneous) in your question.
      C++ can handle as much as you can program it to handle.
      >
      >
      >
      <code\>
      istringstream(o p5) >std::hex >num7;
      istringstream(o p6) >std::hex >num8;
      cout << op5 << "-" << op6 << "=";
      num9= num7-num8;
      cout.flags(ios: :hex);
      cout << uppercase << hex << num9 << endl;
      </code>
      >
      What's the type of 'num7' and 'num8'? Are they large enough to contain
      the external representations you trow at them?
      >
      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask
      I declared num7 and num8 as type long after splitting them from string
      where they were combined separated by the operator sign '-';
      As I said the smaller values work well such as 100*AA=AA00. I was told
      something about limiting the variables to less than 40 digits but I
      dont know how that plays into it.

      Thanks

      Comment

      • Victor Bazarov

        #4
        Re: string operations

        j_depp_99@yahoo .com wrote:
        On Nov 21, 3:29 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
        >j_depp...@yaho o.com wrote:
        >>My program reads in hex values from a file and then performs
        >>arithmetic operations on them. All works well except when I use
        >>large hex values and my answers are incorrect. For example
        >>1000000000000 0-1 should give
        >>FFFFFFFFFFF FF but it does not. I considered converting to decimal to
        >>solve my problem but I'm not sue how to convert my string variable
        >>into a decimal value.
        >>Here's a snippet of my code:
        >>I first convert my two strings into hex, then perform the
        >>subtraction , but when I check the value of num7, it does not display
        >>1000000000000 0. The other operations also involve large hexadecimal
        >>values. Is there perhaps a limit on the length C++, can handle?
        >>
        >I believe the comma is misplaced (or just exraneous) in your
        >question. C++ can handle as much as you can program it to handle.
        >>
        >>
        >>
        >><code\>
        >> istringstream(o p5) >std::hex >num7;
        You don't even check any errors here. That's bad. Give num7 some
        value (not what you intend to read, anyway, like -123456), and then
        see if the value changes after the read operation. Or you can just
        do

        if (!(istringstrea m(op5) >std::hex >num7))
        cerr << "Error converting " << op5 << endl;
        >> istringstream(o p6) >std::hex >num8;
        >> cout << op5 << "-" << op6 << "=";
        >> num9= num7-num8;
        >> cout.flags(ios: :hex);
        >> cout << uppercase << hex << num9 << endl;
        >></code>
        >>
        >What's the type of 'num7' and 'num8'? Are they large enough to
        >contain the external representations you trow at them?
        >>
        >V
        >--
        >Please remove capital 'A's when replying by e-mail
        >I do not respond to top-posted replies, please don't ask
        >
        I declared num7 and num8 as type long
        Hate to break it to you like that, but unless your 'long' is 64 bits,
        the largest hex number it can represent is 7FFFFFFF. Attempting to
        read 10000000000000 into it will most certainly fail.
        after splitting them from string
        where they were combined separated by the operator sign '-';
        As I said the smaller values work well such as 100*AA=AA00. I was told
        something about limiting the variables to less than 40 digits but I
        dont know how that plays into it.
        Not sure what you're talking about.

        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask


        Comment

        • j_depp_99@yahoo.com

          #5
          Re: string operations

          On Nov 21, 3:59 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
          j_depp...@yahoo .com wrote:
          On Nov 21, 3:29 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
          j_depp...@yahoo .com wrote:
          >My program reads in hex values from a file and then performs
          >arithmetic operations on them. All works well except when I use
          >large hex values and my answers are incorrect. For example
          >100000000000 00-1 should give
          >FFFFFFFFFFFF F but it does not. I considered converting to decimal to
          >solve my problem but I'm not sue how to convert my string variable
          >into a decimal value.
          >Here's a snippet of my code:
          >I first convert my two strings into hex, then perform the
          >subtraction, but when I check the value of num7, it does not display
          >10000000000000 . The other operations also involve large hexadecimal
          >values. Is there perhaps a limit on the length C++, can handle?
          >
          I believe the comma is misplaced (or just exraneous) in your
          question. C++ can handle as much as you can program it to handle.
          >
          ><code\>
          > istringstream(o p5) >std::hex >num7;
          >
          You don't even check any errors here. That's bad. Give num7 some
          value (not what you intend to read, anyway, like -123456), and then
          see if the value changes after the read operation. Or you can just
          do
          >
          if (!(istringstrea m(op5) >std::hex >num7))
          cerr << "Error converting " << op5 << endl;
          >
          >
          >
          >
          >
          > istringstream(o p6) >std::hex >num8;
          > cout << op5 << "-" << op6 << "=";
          > num9= num7-num8;
          > cout.flags(ios: :hex);
          > cout << uppercase << hex << num9 << endl;
          ></code>
          >
          What's the type of 'num7' and 'num8'? Are they large enough to
          contain the external representations you trow at them?
          >
          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask
          >
          I declared num7 and num8 as type long
          >
          Hate to break it to you like that, but unless your 'long' is 64 bits,
          the largest hex number it can represent is 7FFFFFFF. Attempting to
          read 10000000000000 into it will most certainly fail.
          >
          after splitting them from string
          where they were combined separated by the operator sign '-';
          As I said the smaller values work well such as 100*AA=AA00. I was told
          something about limiting the variables to less than 40 digits but I
          dont know how that plays into it.
          >
          Not sure what you're talking about.
          >
          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask- Hide quoted text -
          >
          - Show quoted text -- Hide quoted text -
          >
          - Show quoted text -
          Thanks. I added test values in for num7 and it handles the operation.
          But my real problem is trying to use longer values than 7FFFFFFF.
          Is there a way around this?

          Comment

          • red floyd

            #6
            Re: string operations

            j_depp_99@yahoo .com wrote:
            On Nov 21, 3:59 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
            >j_depp...@yaho o.com wrote:
            >>On Nov 21, 3:29 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
            >>>j_depp...@ya hoo.com wrote:
            >>>>My program reads in hex values from a file and then performs
            >>>>arithmeti c operations on them. All works well except when I use
            >>>>large hex values and my answers are incorrect. For example
            >>>>10000000000 000-1 should give
            >>>>FFFFFFFFFFF FF but it does not. I considered converting to decimal to
            >>>>solve my problem but I'm not sue how to convert my string variable
            >>>>into a decimal value.
            >>>>Here's a snippet of my code:
            >>>>I first convert my two strings into hex, then perform the
            >>>>subtraction , but when I check the value of num7, it does not display
            >>>>10000000000 000. The other operations also involve large hexadecimal
            >>>>values. Is there perhaps a limit on the length C++, can handle?
            >>>I believe the comma is misplaced (or just exraneous) in your
            >>>question. C++ can handle as much as you can program it to handle.
            >>>><code\>
            >>>> istringstream(o p5) >std::hex >num7;
            >You don't even check any errors here. That's bad. Give num7 some
            >value (not what you intend to read, anyway, like -123456), and then
            >see if the value changes after the read operation. Or you can just
            >do
            >>
            > if (!(istringstrea m(op5) >std::hex >num7))
            > cerr << "Error converting " << op5 << endl;
            >>
            >>
            >>
            >>
            >>
            >>>> istringstream(o p6) >std::hex >num8;
            >>>> cout << op5 << "-" << op6 << "=";
            >>>> num9= num7-num8;
            >>>> cout.flags(ios: :hex);
            >>>> cout << uppercase << hex << num9 << endl;
            >>>></code>
            >>>What's the type of 'num7' and 'num8'? Are they large enough to
            >>>contain the external representations you trow at them?
            >>>V
            >>>--
            >>>Please remove capital 'A's when replying by e-mail
            >>>I do not respond to top-posted replies, please don't ask
            >>I declared num7 and num8 as type long
            >Hate to break it to you like that, but unless your 'long' is 64 bits,
            >the largest hex number it can represent is 7FFFFFFF. Attempting to
            >read 10000000000000 into it will most certainly fail.
            >>
            >>after splitting them from string
            >>where they were combined separated by the operator sign '-';
            >>As I said the smaller values work well such as 100*AA=AA00. I was told
            >>something about limiting the variables to less than 40 digits but I
            >>dont know how that plays into it.
            >Not sure what you're talking about.
            >>
            >V
            >--
            >Please remove capital 'A's when replying by e-mail
            >I do not respond to top-posted replies, please don't ask- Hide quoted text -
            >>
            >- Show quoted text -- Hide quoted text -
            >>
            >- Show quoted text -
            >
            Thanks. I added test values in for num7 and it handles the operation.
            But my real problem is trying to use longer values than 7FFFFFFF.
            Is there a way around this?
            google for a "bigint" or "multiple precision" library.

            Comment

            • Victor Bazarov

              #7
              Re: string operations

              red floyd wrote:
              j_depp_99@yahoo .com wrote:
              >[..]
              >Thanks. I added test values in for num7 and it handles the operation.
              >But my real problem is trying to use longer values than 7FFFFFFF.
              >Is there a way around this?
              >
              google for a "bigint" or "multiple precision" library.
              On an off-chance your OS/compiler/hardware has some support for 64-bit
              integrals, check your compiler manual for something like __int64 or
              int64_t or some other built-in type with 64 in it.

              V
              --
              Please remove capital 'A's when replying by e-mail
              I do not respond to top-posted replies, please don't ask


              Comment

              • David Harmon

                #8
                Re: string operations

                On Wed, 21 Nov 2007 13:07:24 -0800 (PST) in comp.lang.c++,
                j_depp_99@yahoo .com wrote,
                >Thanks. I added test values in for num7 and it handles the operation.
                >But my real problem is trying to use longer values than 7FFFFFFF.
                >Is there a way around this?
                Would you believe, choose a compiler that supports 64-bit arithmetic
                with "long long"? http://digitalmars.com/


                Comment

                • James Kanze

                  #9
                  Re: string operations

                  On Nov 21, 9:59 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
                  j_depp...@yahoo .com wrote:
                  [...]
                  > istringstream(o p5) >std::hex >num7;
                  You don't even check any errors here. That's bad. Give num7 some
                  value (not what you intend to read, anyway, like -123456), and then
                  see if the value changes after the read operation.
                  That is *not* the way to check for errors. If only because
                  there is no value that you can give it that cannot occur in
                  input.
                  Or you can just do
                  if (!(istringstrea m(op5) >std::hex >num7))
                  cerr << "Error converting " << op5 << endl;
                  That's the correct way. Formally, if the input overflows, it is
                  undefined behavior, but from a quality of implementation point
                  of view, you should get an error.
                  > istringstream(o p6) >std::hex >num8;
                  > cout << op5 << "-" << op6 << "=";
                  > num9= num7-num8;
                  > cout.flags(ios: :hex);
                  > cout << uppercase << hex << num9 << endl;
                  ></code>
                  What's the type of 'num7' and 'num8'? Are they large
                  enough to contain the external representations you trow at
                  them?
                  I declared num7 and num8 as type long
                  Hate to break it to you like that, but unless your 'long' is
                  64 bits, the largest hex number it can represent is 7FFFFFFF.
                  Actually, anything larger than 33 bits suffices. There are
                  definitely machines out there with 36 bit longs, and there have
                  probably been other sizes (e.g. 48 bits) as well.
                  Attempting to read 10000000000000 into it will most certainly
                  fail.
                  Regretfully no. It's undefined behavior.

                  Of course, if by "fail" you simply mean you won't get the
                  correct value in the variable, of course, then you're right. A
                  32 bit long can't hold such a value.

                  --
                  James Kanze (GABI Software) email:james.kan ze@gmail.com
                  Conseils en informatique orientée objet/
                  Beratung in objektorientier ter Datenverarbeitu ng
                  9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                  Comment

                  • James Kanze

                    #10
                    Re: string operations

                    On Nov 21, 10:30 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
                    red floyd wrote:
                    j_depp...@yahoo .com wrote:
                    [..]
                    Thanks. I added test values in for num7 and it handles the operation.
                    But my real problem is trying to use longer values than 7FFFFFFF.
                    Is there a way around this?
                    google for a "bigint" or "multiple precision" library.
                    On an off-chance your OS/compiler/hardware has some support
                    for 64-bit integrals, check your compiler manual for something
                    like __int64 or int64_t or some other built-in type with 64 in
                    it.
                    In C, there is a type long long which is guaranteed to be at
                    least 64 bits. This type will also be part of the next version
                    of the C++ standard, and I don't know of a compiler today which
                    doesn't support it.

                    --
                    James Kanze (GABI Software) email:james.kan ze@gmail.com
                    Conseils en informatique orientée objet/
                    Beratung in objektorientier ter Datenverarbeitu ng
                    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                    Comment

                    Working...