Advertisement
Guest User

partea2.2

a guest
Oct 17th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. /*
  7. //baza8
  8. void baza10tob (int x, int b)
  9. {
  10. if (x)
  11. {
  12. baza10tob (x / b, b);
  13. cout << x % b;
  14. }
  15.  
  16. }
  17. */
  18.  
  19. /*
  20. //baza16
  21. void baza10tob(int x, int b)
  22. {
  23. if(x)
  24. {
  25. baza10tob(x / b , b);
  26. if(x % b < 10)
  27. cout<< x % b;
  28. else
  29. cout<<(char)('A' + x % b % 10);
  30. }
  31.  
  32. }
  33.  
  34. */
  35.  
  36. //din baza b in baza 10
  37. int putere(int x, int p)
  38. {
  39. int pow=1;
  40. for(int i=0;i<p;i++)
  41. pow *=x;
  42. return pow;
  43.  
  44. }
  45.  
  46. int baza_b_to_10(const char s[], int b )
  47. {
  48. int sum=0;
  49. int n = strlen(s);
  50. for(int i=0; i<n; i++)
  51. if(s[i] <= '9')
  52. sum += (s[i] - '0') * putere(b, n - i - 1);
  53. else
  54. sum += (s[i] - 'A' + 10) * putere(b, n - i - 1);
  55.  
  56. return sum;
  57. }
  58.  
  59.  
  60. int main (int argc, char *argv[])
  61. {
  62.  
  63. //int nr=0xA2B4;
  64. //cout<<nr;
  65. //cout<< dec << 0xA2B4; //sa afisese in baza 10
  66.  
  67.  
  68. //cout << oct << 831466 << endl;
  69.  
  70. //baza10tob(831466,8);
  71.  
  72. //baza10tob (831466, 16);
  73.  
  74. cout<<baza_b_to_10("CAFEA", 16);
  75.  
  76. return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement