473,385 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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(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
Nov 21 '07 #1
9 1948
j_*******@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(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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 21 '07 #2
On Nov 21, 3:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.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(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
--
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
Nov 21 '07 #3
j_*******@yahoo.com wrote:
On Nov 21, 3:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.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(op5) >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 (!(istringstream(op5) >std::hex >num7))
cerr << "Error converting " << op5 << endl;
>> 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
--
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
Nov 21 '07 #4
On Nov 21, 3:59 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
j_depp...@yahoo.com wrote:
On Nov 21, 3:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.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(op5) >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 (!(istringstream(op5) >std::hex >num7))
cerr << "Error converting " << op5 << endl;


> 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
--
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?
Nov 21 '07 #5
j_*******@yahoo.com wrote:
On Nov 21, 3:59 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>j_depp...@yahoo.com wrote:
>>On Nov 21, 3:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.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(op5) >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 (!(istringstream(op5) >std::hex >num7))
cerr << "Error converting " << op5 << endl;


>>>> 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
--
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.
Nov 21 '07 #6
red floyd wrote:
j_*******@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
Nov 21 '07 #7
On Wed, 21 Nov 2007 13:07:24 -0800 (PST) in comp.lang.c++,
j_*******@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/
Nov 22 '07 #8
On Nov 21, 9:59 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
j_depp...@yahoo.com wrote:
[...]
> istringstream(op5) >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 (!(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.
> 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?
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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 22 '07 #9
On Nov 21, 10:30 pm, "Victor Bazarov" <v.Abaza...@comAcast.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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 22 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Oliver S. | last post by:
I've developed a string-class that holds the string in an array which is a member-variable of the class and that has maximum-size which is con- figurable through a template-parameter. If any...
10
by: cmk128 | last post by:
Hi I am going to implement a new string class for my os, do you have any idea to enhance of standard c++ string class? If compare c++ string class with CS++ CString class and java String class,...
32
by: Wolfgang Draxinger | last post by:
I understand that it is perfectly possible to store UTF-8 strings in a std::string, however doing so can cause some implicaions. E.g. you can't count the amount of characters by length() | size()....
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
2
by: Dan Schumm | last post by:
I'm relatively new to regular expressions and was looking for some help on a problem that I need to solve. Basically, given an HTML string, I need to highlight certain words within the text of the...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
23
by: comp.lang.tcl | last post by:
I have a TCL proc that needs to convert what might be a list into a string to read consider this: ]; # OUTPUTS Hello World which is fine for PHP ]; # OUTPUT {{-Hello}} World, which PHP...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
25
by: Bala2508 | last post by:
Hi, I have a C++ application that extensively uses std::string and std::ostringstream in somewhat similar manner as below std::string msgHeader; msgHeader = "<"; msgHeader += a; msgHeader...
3
by: yogi_bear_79 | last post by:
I'm sure I have a few things wrong here. But I am stuck on how to do a recurring search. Also my statement cin >quote; acts weird. If I enter more than one word it blows right past cin >findMe;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.