Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- #include <cmath>
- using namespace std;
- #define MAX 50
- int countDigit(long long n){
- return floor(log10(n)+1);
- }
- char* hexToDec(char hex[]){
- int r, total=0;
- int length = strlen(hex);
- for(int i=0;i<hex[i]!='\0';i++){
- length--;
- if(hex[i]>='0' && hex[i] <='9'){
- r = hex[i] - 48;
- }else if(hex[i]>='a' && hex[i]<='f'){
- r = hex[i] - 87;
- }else if(hex[i]>='A' && hex[i]<='F'){
- r = hex[i] - 55;
- }
- total += r*pow(16, length);
- }
- int l = countDigit(total);
- char *result = new char[l];
- for(int i=0;i<l;i++){
- result[i] = total%10 + '0';
- total/=10;
- }
- return result;
- }
- void print(char* num){
- cout << "Result : ";
- for(int i=strlen(num)-1;i>=0;i--){
- cout << num[i];
- }
- cout << endl;
- }
- int main(){
- char num[MAX];
- cout << "Enter a hexadecimal number : ";
- while(cin >> num){
- char* res = hexToDec(num);
- print(res);
- free(res);
- cout << "Enter a hexadecimal number : ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement