[C++]Conversioni numeriche
Breve post illustrativo su come convertire degli interi positivi in base 2,8 e 16. Ometto la definizione di number e l'inclusione degli header. Chi volesse saperne di più commenti qui sotto.
cout.setf(ios::showbase); // mostra la base es. 0x per HEX
cout << "The number in hex is " << hex << number << endl;
cout.unsetf(ios_base::showbase); // non mostra più la base
cout << "The number in oct is " << oct << number << endl;
cout << "The number in binary is ";
binary(number);
cout << endl;
La funzione binary(int) viene così definita:
void binary(int number) {
int remainder;
if(number <= 1) {
cout << number;
return;
}
remainder = number%2;
binary(number >> 1);
cout << remainder;
}
14:00
|
Categorie:
c++
|
This entry was posted on 14:00
and is filed under
c++
.
You can follow any responses to this entry through
the RSS 2.0 feed.
You can leave a response,
or trackback from your own site.
Iscriviti a:
Commenti sul post (Atom)
0 comments:
Posta un commento