Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- #include <cstring>
- using namespace std;
- /*
- //baza8
- void baza10tob (int x, int b)
- {
- if (x)
- {
- baza10tob (x / b, b);
- cout << x % b;
- }
- }
- */
- /*
- //baza16
- void baza10tob(int x, int b)
- {
- if(x)
- {
- baza10tob(x / b , b);
- if(x % b < 10)
- cout<< x % b;
- else
- cout<<(char)('A' + x % b % 10);
- }
- }
- */
- //din baza b in baza 10
- int putere(int x, int p)
- {
- int pow=1;
- for(int i=0;i<p;i++)
- pow *=x;
- return pow;
- }
- int baza_b_to_10(const char s[], int b )
- {
- int sum=0;
- int n = strlen(s);
- for(int i=0; i<n; i++)
- if(s[i] <= '9')
- sum += (s[i] - '0') * putere(b, n - i - 1);
- else
- sum += (s[i] - 'A' + 10) * putere(b, n - i - 1);
- return sum;
- }
- int main (int argc, char *argv[])
- {
- //int nr=0xA2B4;
- //cout<<nr;
- //cout<< dec << 0xA2B4; //sa afisese in baza 10
- //cout << oct << 831466 << endl;
- //baza10tob(831466,8);
- //baza10tob (831466, 16);
- cout<<baza_b_to_10("CAFEA", 16);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement