
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Convert from any base to decimal and vice versa in C++
In this tutorial, we will be discussing a program to convert from any base to a decimal and vice versa.
For this we will be provided with an integer and its base. Our task is to convert the number to its decimal equivalent. Further we will also be performing the reverse of this procedure as well.
Example
#include <stdio.h> #include <string.h> //returning values of a character int val(char c) { if (c >= '0' && c <= '9') return (int)c - '0'; else return (int)c - 'A' + 10; } //converting number to decimal equivalent int convert_decimal(char *str, int base) { int len = strlen(str); int power = 1; int num = 0; int i; for (i = len - 1; i >= 0; i--) { if (val(str[i]) >= base) { printf("Invalid Number"); return -1; } num += val(str[i]) * power; power = power * base; } return num; } int main() { char str[] = "11A"; int base = 16; printf("Decimal equivalent of %s in base %d is " " %d\n", str, base, convert_decimal(str, base)); return 0; }
Ouptut
Decimal equivalent of 11A in base 16 is 282
Now performing the reverse of the operation.
Example
#include <stdio.h> #include <string.h> //returning values of a character char reVal(int num) { if (num <= 0 && num >= 9) return (char)(num + '0'); else return (char)(num - 10 + 'A'); } //reversing a given string void reverse_string(char *str) { int len = strlen(str); int i; for (i = 0; i < len/2; i++) { char temp = str[i]; str[i] = str[len-i-1]; str[len-i-1] = temp; } } //converting to equivalent number with base 'b' char* convert_base(char res[], int base, int inputNum) { int index = 0; while (inputNum > 0) { res[index++] = reVal(inputNum % base); inputNum /= base; } res[index] = '\0'; //reversing the result reverse_string(res); return res; } int main() { int inputNum = 282, base = 16; char res[100]; printf("Equivalent of %d in base %d is "" %s\n", inputNum, base, convert_base(res, base, inputNum)); return 0; }
Output
Equivalent of 282 in base 16 is 11A
- Related Articles
- C++ Program to Convert Binary Number to Decimal and vice-versa
- C++ Program to convert Octal Number to Decimal and vice-versa
- Binary to decimal and vice-versa in Python
- Convert string to DateTime and vice-versa in Python
- How to Convert Decimal Number to Binary/Octal/Hex Number or Vice Versa in Excel?
- How to convert String to StringBuilder and vice versa Java?
- How to convert an integer to hexadecimal and vice versa in C#?
- How to convert an array to Set and vice versa in Java?
- C++ Program to Convert Binary Number to Octal and vice-versa
- How to Convert Time Format from 12 Hour to 24 Hour and Vice Versa in Excel?
- Golang program to convert the arraylist into string and vice-versa
- How to Convert a String to Hexadecimal and vice versa format in java?
- C++ Program to Convert Km/hr to miles/hr and vice versa
- How to Convert Letter to Number or Vice Versa in Excel?
- Java Program to Convert the LinkedList into an Array and vice versa

Advertisements