std::dec, std::hex, std::oct
提供: cppreference.com
ヘッダ <ios> で定義 | ||
std::ios_base& dec(std::ios_base& str ); | (1) | |
std::ios_base& hex(std::ios_base& str ); | (2) | |
std::ios_base& oct(std::ios_base& str ); | (3) | |
整数の入出力に対するデフォルトの基数を変更します。
1) str.setf(std::ios_base::dec, std::ios_base::basefield) を呼んだかのように、ストリーム str
の basefield
を dec
に設定します。
2) str.setf(std::ios_base::hex, std::ios_base::basefield) を呼んだかのように、ストリーム str
の basefield
を hex
に設定します。
3) str.setf(std::ios_base::oct, std::ios_base::basefield) を呼んだかのように、ストリーム str
の basefield
を oct
に設定します。
これは入出力マニピュレータです。 std::basic_ostream 型の任意の out
に対する out << std::hex のような式や std::basic_istream 型の任意の in
に対する in >> std::hex のような式で呼ぶことができます。
目次 |
[編集]引数
str | - | 入出力ストリームへの参照 |
[編集]戻り値
str
(操作後のストリームへの参照)。
[編集]例
Run this code
#include <iostream>#include <sstream>int main(){std::cout<<"The number 42 in octal: "<< std::oct<<42<<'\n'<<"The number 42 in decimal: "<< std::dec<<42<<'\n'<<"The number 42 in hex: "<< std::hex<<42<<'\n';int n;std::istringstream("2A")>> std::hex>> n;std::cout<< std::dec<<"Parsing \"2A\" as hex gives "<< n <<'\n';// the output base is sticky until changedstd::cout<< std::hex<<"42 as hex gives "<<42<<" and 21 as hex gives "<<21<<'\n';}
出力:
The number 42 in octal: 52 The number 42 in decimal: 42 The number 42 in hex: 2a Parsing "2A" as hex gives 42 42 as hex gives 2a and 21 as hex gives 15
[編集]関連項目
整数の入出力に使用される基数を変更します (関数) | |
数値の基数を表すために接頭辞を使用するかどうか制御します (関数) |