Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > OO C++ Workshop: Converting a String to a Numeric Type

Reply
Thread Tools

OO C++ Workshop: Converting a String to a Numeric Type

 
 
CoreyWhite
Guest
Posts: n/a
 
      04-05-2007
Problem:
You have numbers in string format, but you need to convert them to a
numeric type, such as an int or float.

Solution:

You can do this with the standard library functions. The functions
strtol, strtod, and strtoul, defined in <cstdlib>, convert a null-
terminated character string to a long int, double, or unsigned long.
You can use them to convert numeric strings of any base to a numeric
type (Even hexidecimal). The code I wrote here demonstrates this
function, within an object oriented design model. You can use it for
converting hexadecimal strings to a long integer.

I'll also include the non-object oriented code so you can see how this
can be done without using many of the great tools in C++, as the point
of this workshop is to compare object oriented algorithms to standard
C code.


#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>

using namespace std;

class Hex : public vector<string>
{

public:
Hex() {
cout << "Converting hexadecimal strings to integers."<<endl<<endl;
}
inline void operator()(const string &hexStr)
{
push_back(hexStr);
}

inline int operator[](const int &n)
{
string hexStr = at(n);

char *offset;
if (hexStr.length() >2){
if (hexStr[0] == '0' && hexStr[1] == 'x') {
return strtol(hexStr.c_str(), &offset, 0);
}
}
return strtol(hexStr.c_str(), &offset, 16);

}

};

int main( void ) {
Hex myHex;
myHex("0x12AB");
myHex("12AB");
for(int cnt=0; cnt < myHex.size(); cnt++){
cout << myHex[cnt] << endl;
}

return 0;
}


==================
ANOTHER PROGRAM
==================

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

long hex2int(const string& hexStr) {
char *offset;
if (hexStr.length() > 2 ) {
if ( hexStr[0] == '0' && hexStr[1] == 'x' ) {
return strtol(hexStr.c_str(), &offset, 0 );
}
}
return strtol(hexStr.c_str(), &offset, 16);
}

int main(){
string str1 = "0x12AB";
cout << hex2int(str1) << endl;
string str2 = "12AB";
cout << hex2int(str2) << endl;
return 0;
}

=======
END
======

The program should output the hexidecimal number converted to 4779,
twice!!!

Hope you enjoy, and if you have any questions about my object oriented
design model, and I would be glad to explain what I am doing with it!

 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      04-06-2007
On 5 Apr 2007 11:57:48 -0700 in comp.lang.c++, "CoreyWhite"
<(E-Mail Removed)> wrote,
>Newsgroups: alt.magick,alt.native,alt.religion.wicca,comp.lang .c++


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-06-2007
David Harmon wrote:
> On 5 Apr 2007 11:57:48 -0700 in comp.lang.c++, "CoreyWhite"
> <(E-Mail Removed)> wrote,
>> Newsgroups: alt.magick,alt.native,alt.religion.wicca,comp.lang .c++


It's time for folks to stop feeding that troll until she learns
to use Usenet properly.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error converting data type nvarchar to numeric Adam_Cogswell ASP .Net 1 02-29-2008 04:12 PM
int to numeric numeric(18,2) ? jobs ASP .Net 2 07-22-2007 12:32 AM
Arithmetic overflow error converting numeric to data type numeric. darrel ASP .Net 4 07-19-2007 09:57 PM
check if string contains numeric, and check string length of numeric value ief@specialfruit.be C++ 5 06-30-2005 01:08 PM
Error converting data type varchar to numeric - stored procedures luna ASP .Net 1 02-06-2004 01:23 PM



Advertisments