Anilto

Conversor decimal hexa, octal, binario.

Oct 17th, 2020
829
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*  decimal para hexadecimal, binario e octal
  2.     Jose Anilto
  3. */
  4. #include <iostream>
  5. #include <cstring>
  6.  
  7.  
  8. using namespace std;
  9.  
  10.  
  11. string converte(int n, string x, int base) {
  12.  
  13.     string v = {"0123456789ABCDEF"};
  14.     return (n < base ? (v[n] + x) : converte(n/base, v[n%base] + x, base));
  15.  
  16. }
  17.  
  18.  
  19. int main () {
  20.  
  21.     int nd ;
  22.     string y = "";
  23.     cout << "\n Digite um numero: "; cin >> nd;
  24.     cout << "\n Numero digitado: " << nd << "\n";
  25.  
  26.     cout << "\n Hexadecimal: " << converte(nd, y, 16) << "\n";
  27.     y = "";
  28.     cout << "\n Binario: " << converte(nd, y, 2) << "\n";
  29.     y = "";
  30.     cout << "\n Octal: " << converte(nd, y, 8) << "\n\n";
  31.     return 0;
  32. }
RAW Paste Data