std:: dec, std:: hex, std:: oct
|
Definiert im Header
<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) | |
Modifiziert die Standardzahlbasis für die Ganzzahl-Ein-/Ausgabe.
basefield
des Streams
str
auf
dec
als ob durch Aufruf von
str.
setf
(
std::
ios_base
::
dec
,
std::
ios_base
::
basefield
)
.
basefield
des Streams
str
auf
hex
wie durch den Aufruf von
str.
setf
(
std::
ios_base
::
hex
,
std::
ios_base
::
basefield
)
.
basefield
des Streams
str
auf
oct
, als ob durch Aufruf von
str.
setf
(
std::
ios_base
::
oct
,
std::
ios_base
::
basefield
)
.
Dies ist ein I/O-Manipulator. Er kann mit einem Ausdruck wie
out
<<
std
::
hex
für jedes
out
vom Typ
std::basic_ostream
oder mit einem Ausdruck wie
in
>>
std
::
hex
für jedes
in
vom Typ
std::basic_istream
aufgerufen werden.
Inhaltsverzeichnis |
Parameter
| str | - | Referenz auf I/O-Stream |
Rückgabewert
str (Referenz auf den Stream nach der Manipulation).
Beispiel
#include <bitset> #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 changed std::cout << std::hex << "42 as hex gives " << 42 << " and 21 as hex gives " << 21 << '\n'; // Note: there is no I/O manipulator that sets up a stream to print out // numbers in binary format (e.g. bin). If binary output is necessary // the std::bitset trick can be used: std::cout << "The number 42 in binary: " << std::bitset<8>{42} << '\n'; }
Ausgabe:
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 The number 42 in binary: 00101010
Siehe auch
|
ändert die Basis für Ganzzahl-Ein-/Ausgabe
(Funktion) |
|
|
steuert, ob Präfix zur Angabe der numerischen Basis verwendet wird
(Funktion) |