2010-04-11 5 views
0

주소 예 : 0x003533, 해당 문자열을 사용하지만 긴 필요가 있지만 그것을 수행하는 방법을 모릅니다 : S 누구나 해결책이 있습니까?C++ 주소 문자열 -> long

문자열 : "0x003533"~ long 0x003533 ??

답변

5

사용 strtol() 같이 :

 
#include <cstdlib> 
#include <string> 

// ... 
{ 
    // ... 
    // Assume str is an std::string containing the value 
    long value = strtol(str.c_str(),0,0); 
    // ... 
} 
// ... 
3
#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 
} 

편집 : Potatocorn이 코멘트에 말했듯이 다음과 같이

, 당신은 또한 boost::lexical_cast를 사용할 수 있습니다

long x = 0L; 
try { 
    x = lexical_cast<long>("0x003533"); 
} 
catch(bad_lexical_cast const & blc) { 
    // handle the exception 
} 
+0

AKA 'boost :: lexical_cast' – Potatoswatter