0

Is there any simple way to convert decimal system to binary system like i did with oct and hex?

#include <iostream> using namespace std; int main () { unsigned short A; cout << "A = "; cin >> A; cout << oct << A<<endl; cout<< hex<<A<<endl; return 0; }

26th Oct 2020, 3:04 PM
Dr. Alien
Dr. Alien - avatar
2 Antworten
26th Oct 2020, 3:26 PM
John Meow
John Meow - avatar
0
Unfortunately setbase is working just with bases 8, 10 and 16 or default (the base in iostream is not treated as a number, but as a set of flags). Your options (as far as i know) are: a) writing your own output wrapper b) output a bitset<WIDTH>(n) https://en.cppreference.com/w/cpp/utility/bitset/operator_ltltgtgt2 c) using itoa from stdlib.h, which, while available in most compilers, is nonstandard http://www.cplusplus.com/reference/cstdlib/itoa/
26th Oct 2020, 4:36 PM
Volodymyr Chelnokov
Volodymyr Chelnokov - avatar