No.1ベストアンサー
- 回答日時:
原則として無理なので、strtolを使ってください。
int n;
errno = 0;
char* endptr;
long temp = std::strtol(ss.c_str(), &endptr, 0);
if (errno != 0 || temp < std::numeric_limits<int>::min() || std::numeric_limits<int>::max() < temp || *endptr != '\0')
{
// エラー処理
}
No.2
- 回答日時:
こんな感じで無理すれば出来なくは無いです。
#include <sstream>
#include <iomanip>
#include <boost/lexical_cast.hpp>
template<typename DST>
struct hex_to
{
operator DST(void) const
{
return value;
}
DST value;
};
template<typename DST>
std::istream& operator>>( std::istream& ist , hex_to<DST>& h )
{
std::string s;
ist >> std::hex >> h.value;
return ist;
}
int main(int argc, char* argv[])
{
int i = boost::lexical_cast< hex_to<int> >("0xff");
std::cout << i << std::endl;
getchar();
return 0;
}
ただ、ちょっと複雑すぎるので出来れば避けたほうが良いような気はします。
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・人生のプチ美学を教えてください!!
- ・10秒目をつむったら…
- ・あなたの習慣について教えてください!!
- ・牛、豚、鶏、どれか一つ食べられなくなるとしたら?
- ・【大喜利】【投稿~9/18】 おとぎ話『桃太郎』の知られざるエピソード
- ・街中で見かけて「グッときた人」の思い出
- ・「一気に最後まで読んだ」本、教えて下さい!
- ・幼稚園時代「何組」でしたか?
- ・激凹みから立ち直る方法
- ・1つだけ過去を変えられるとしたら?
- ・【あるあるbot連動企画】あるあるbotに投稿したけど採用されなかったあるある募集
- ・【あるあるbot連動企画】フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
- ・映画のエンドロール観る派?観ない派?
- ・海外旅行から帰ってきたら、まず何を食べる?
- ・誕生日にもらった意外なもの
- ・天使と悪魔選手権
- ・ちょっと先の未来クイズ第2問
- ・【大喜利】【投稿~9/7】 ロボットの住む世界で流行ってる罰ゲームとは?
- ・推しミネラルウォーターはありますか?
- ・都道府県穴埋めゲーム
- ・この人頭いいなと思ったエピソード
- ・準・究極の選択
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
setw
-
構造体配列のvectorへの変換と...
-
wstringの内容を表示させたい
-
C++でShowCursorを使いたい。
-
コンパイルできません
-
VHDLのsignedとunsignedの違いは?
-
string型の固定長文字列を配列...
-
VC++で文字列から任意の文字を...
-
#defineの使い方について
-
switch文のエラーについて
-
指定した文字を削除したい
-
構文エラーが出ているのですが...
-
JPEGやPNGが読めるLoadImage関数
-
C++でのmath.hやstdio.hの使用...
-
C言語の漢字のホルダーは、chdi...
-
STL string::findで見つからな...
-
2÷3などの余りについて
-
信頼区間の1.96や1.65ってどこ...
-
「Aに対するBの割合」と「Aに対...
-
C言語での引数の省略方法
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
switch文のエラーについて
-
構文エラーが出ているのですが...
-
なぜ、C++の標準ヘッダをインク...
-
gccでコンパイル時のエラー
-
空ENTERの判別
-
【C++】ヘッダ内でstringを格納...
-
snprintfが使用できない。
-
enumの値から定義名を文字列化...
-
#include "fstream.h"
-
C++で、テキストファイルを一行...
-
std::wstringのメモリリークに...
-
C++での <iostream.h>と<iostre...
-
VC++で文字列から任意の文字を...
-
#defineの使い方について
-
wstringの主力
-
vc++の使い方について
-
std::map の const 修飾について
-
string型のフォーマット書式指...
-
構造体配列のvectorへの変換と...
-
指定した文字を削除したい
おすすめ情報