0

アドレスの例を取得しました: 0x003533 、これは文字列ですが、それを使用するには LONG である必要がありますが、その方法がわかりません:S 誰かに解決策がありますか?

文字列: "0x003533" から long 0x003533 ??

4

2 に答える 2

5

次のようにstrtol()を使用します。

#include <cstdlib>
#include <文字列>

// ...
{
   // ...
   // str は値を含む std::string であると仮定します
   長い値 = strtol(str.c_str(),0,0);
   // ...
}
// ...
于 2010-04-11T09:25:54.823 に答える
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
}
于 2010-04-11T12:03:25.140 に答える