アドレスの例を取得しました: 0x003533 、これは文字列ですが、それを使用するには LONG である必要がありますが、その方法がわかりません:S 誰かに解決策がありますか?
文字列: "0x003533" から long 0x003533 ??
アドレスの例を取得しました: 0x003533 、これは文字列ですが、それを使用するには LONG である必要がありますが、その方法がわかりません:S 誰かに解決策がありますか?
文字列: "0x003533" から long 0x003533 ??
次のようにstrtol()を使用します。
#include <cstdlib> #include <文字列> // ... { // ... // str は値を含む std::string であると仮定します 長い値 = strtol(str.c_str(),0,0); // ... } // ...
#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
}