string operations

J

j_depp_99

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(op5) >> std::hex >> num7;
istringstream(op6) >> std::hex >> num8;
cout << op5 << "-" << op6 << "=";
num9= num7-num8;
cout.flags(ios::hex);
cout << uppercase << hex << num9 << endl;
</code>

Thanks
 
V

Victor Bazarov

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(op5) >> std::hex >> num7;
istringstream(op6) >> 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
 
J

j_depp_99

I believe the comma is misplaced (or just exraneous) in your question.
C++ can handle as much as you can program it to handle.




What's the type of 'num7' and 'num8'? Are they large enough to contain
the external representations you trow at them?

V

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
 
V

Victor Bazarov

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 (!(istringstream(op5) >> std::hex >> num7))
cerr << "Error converting " << op5 << endl;
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
 
J

j_depp_99

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 (!(istringstream(op5) >> std::hex >> num7))
cerr << "Error converting " << op5 << endl;







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.


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?
 
R

red floyd

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

Victor Bazarov

red said:
[..]
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
 
D

David Harmon

On Wed, 21 Nov 2007 13:07:24 -0800 (PST) in comp.lang.c++,
(e-mail address removed) 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/
 
J

James Kanze

(e-mail address removed) wrote:

[...]
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 (!(istringstream(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.
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.
 
J

James Kanze

red said:
[..]
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.
 

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

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,113
Latest member
Vinay KumarNevatia
Top