views:

120

answers:

2

I got an adress example: 0x003533 , its a string but to use it i need it to be a LONG but i dont know how to do it :S has anybody a solution?

so string: "0x003533" to long 0x003533 ??

+4  A: 

Use strtol() as in:

#include <cstdlib>
#include <string>

// ...
{
   // ...
   // Assume str is an std::string containing the value
   long value = strtol(str.c_str(),0,0);
   // ...
}
// ...
Michael Aaron Safyan
+4  A: 
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main() {
  string s("0x003533");
  long x;
  istringstream(s) >> hex >> x;
  cout << hex << x << endl; // prints 3533
  cout << dec << x << endl; // prints 13619
}

EDIT:

As Potatocorn said in the comments, you can also use boost::lexical_cast as shown below:

long x = 0L;
try {
  x = lexical_cast<long>("0x003533");
}
catch(bad_lexical_cast const & blc) {
  // handle the exception
}
missingfaktor
AKA `boost::lexical_cast`
Potatoswatter