Given a string n and its base b, convert it to decimal. The base of number can be anything such that all digits can be represented using 0 to 9 and A to Z. Value of A is 10, value of B is 11 and so on.
Examples:
Input: b = 2, n = "1100"
Output: 12
Explanation: It is a binary number whose decimal equivalent is 12.Input: b = 16, n = "A"
Output: 10
Explanation: It's a hexadecimal number whose decimal equivalent is 10.
Table of Content
Base Conversion Using Power - O(n Log n) Time and O(1) Space
The idea is to process the digits from right to left, multiply each digit by its corresponding power of the base, and add all the values to obtain the decimal equivalent.
Working of Approach:
- Traverse the given number from right to left, starting from the least significant digit.
- Convert each character into its corresponding numeric value using getValue().
- Compute the power of the base for the current position as power = n - 1 - i.
- Add the contribution of the current digit to the result using res += digit * pow(b, power).
- After processing all the digits, res contains the decimal equivalent of the given base-b number.
#include <cmath>
#include <iostream>
using namespace std;
// Convert a character to its numeric value
int getValue(char ch)
{
if (ch >= '0' && ch <= '9')
{
return ch - '0';
}
return ch - 'A' + 10;
}
int decimalEquivalent(string &s, int b)
{
int n = s.size();
int res = 0;
// Process from right to left
for (int i = n - 1; i >= 0; i--)
{
int digit = getValue(s[i]);
int power = n - 1 - i;
res += digit * pow(b, power);
}
return res;
}
int main()
{
string s = "1A";
int b = 16;
cout << decimalEquivalent(s, b);
return 0;
}
import java.util.Scanner;
public class GFG {
// Convert a character to its numeric value
static int getValue(char ch)
{
if (ch >= '0' && ch <= '9') {
return ch - '0';
}
return ch - 'A' + 10;
}
static int decimalEquivalent(String s, int b)
{
int n = s.length();
int res = 0;
// Process from right to left
for (int i = n - 1; i >= 0; i--) {
int digit = getValue(s.charAt(i));
int power = n - 1 - i;
res += digit * Math.pow(b, power);
}
return res;
}
public static void main(String[] args)
{
String s = "1A";
int b = 16;
System.out.println(decimalEquivalent(s, b));
}
}
import math
# Convert a character to its numeric value
def getValue(ch):
if '0' <= ch <= '9':
return ord(ch) - ord('0')
return ord(ch) - ord('A') + 10
def decimalEquivalent(s, b):
n = len(s)
res = 0
# Process from right to left
for i in range(n - 1, -1, -1):
digit = getValue(s[i])
power = n - 1 - i
res += digit * math.pow(b, power)
return int(res)
if __name__ == '__main__':
s = "1A"
b = 16
print(decimalEquivalent(s, b))
using System;
public class GFG {
// Convert a character to its numeric value
static int getValue(char ch)
{
if (ch >= '0' && ch <= '9') {
return ch - '0';
}
return ch - 'A' + 10;
}
static int decimalEquivalent(string s, int b)
{
int n = s.Length;
int res = 0;
// Process from right to left
for (int i = n - 1; i >= 0; i--) {
int digit = getValue(s[i]);
int power = n - 1 - i;
res += digit * (int)Math.Pow(b, power);
}
return res;
}
public static void Main()
{
string s = "1A";
int b = 16;
Console.WriteLine(decimalEquivalent(s, b));
}
}
"use strict";
// Convert a character to its numeric value
function getValue(ch)
{
if (ch >= "0" && ch <= "9") {
return ch.charCodeAt(0) - "0".charCodeAt(0);
}
return ch.charCodeAt(0) - "A".charCodeAt(0) + 10;
}
function decimalEquivalent(s, b)
{
let n = s.length;
let res = 0;
// Process from right to left
for (let i = n - 1; i >= 0; i--) {
let digit = getValue(s[i]);
let power = n - 1 - i;
res += digit * Math.pow(b, power);
}
return res;
}
// Driver Code
let s = "1A";
let b = 16;
console.log(decimalEquivalent(s, b));
Output
26
Using Left to Right Accumulation - O(n) Time and O(1) Space
The idea is to process the given number from left to right using Horner's Method. For each digit, multiply the current result by the base and add the value of the current digit. After processing all the digits, the final result is the decimal equivalent of the given number.
Working of Approach:
- Initialize res = 0 to store the decimal equivalent of the given number.
- Traverse the string s from left to right and convert each character into its corresponding numeric value using getValue().
- For each digit, multiply the current result by the base (res = res * b) to shift the existing digits one place to the left in base b.
- Add the value of the current digit to the result (res = res * b + digit).
- After all the digits are processed, res contains the decimal equivalent of the given base-b number.
Let us understand with an example:
Input: b = 16, n = "A"
- Initialize res = 0.
- Read 'A', so digit = 10.
- Update res = res * 16 + 10 = 0 * 16 + 10 = 10.
- All digits are processed, so return res = 10.
- Hence, the decimal equivalent of "A" in base 16 is 10.
#include <iostream>
using namespace std;
// Convert a character to its numeric value
int getValue(char ch)
{
if (ch >= '0' && ch <= '9')
{
return ch - '0';
}
return ch - 'A' + 10;
}
int decimalEquivalent(string &s, int b)
{
int res = 0;
// Build the decimal number from left to right
for (char ch : s)
{
int digit = getValue(ch);
res = res * b + digit;
}
return res;
}
int main()
{
string s = "1A";
int b = 16;
cout << decimalEquivalent(s, b);
return 0;
}
import java.util.*;
public class GFG {
// Convert a character to its numeric value
static int getValue(char ch)
{
if (ch >= '0' && ch <= '9') {
return ch - '0';
}
return ch - 'A' + 10;
}
static int decimalEquivalent(String s, int b)
{
int res = 0;
// Build the decimal number from left to right
for (char ch : s.toCharArray()) {
int digit = getValue(ch);
res = res * b + digit;
}
return res;
}
public static void main(String[] args)
{
String s = "1A";
int b = 16;
System.out.println(decimalEquivalent(s, b));
}
}
# Convert a character to its numeric value
def getValue(ch):
if '0' <= ch <= '9':
return ord(ch) - ord('0')
return ord(ch) - ord('A') + 10
def decimalEquivalent(s, b):
res = 0
# Build the decimal number from left to right
for ch in s:
digit = getValue(ch)
res = res * b + digit
return res
if __name__ == "__main__":
s = "1A"
b = 16
print(decimalEquivalent(s, b))
using System;
class GFG {
// Convert a character to its numeric value
static int getValue(char ch)
{
if (ch >= '0' && ch <= '9') {
return ch - '0';
}
return ch - 'A' + 10;
}
static int decimalEquivalent(string s, int b)
{
int res = 0;
// Build the decimal number from left to right
foreach(char ch in s)
{
int digit = getValue(ch);
res = res * b + digit;
}
return res;
}
static void Main()
{
string s = "1A";
int b = 16;
Console.WriteLine(decimalEquivalent(s, b));
}
}
// Convert a character to its numeric value
function getValue(ch)
{
if (ch >= "0" && ch <= "9") {
return ch.charCodeAt(0) - "0".charCodeAt(0);
}
return ch.charCodeAt(0) - "A".charCodeAt(0) + 10;
}
function decimalEquivalent(s, b)
{
let res = 0;
// Build the decimal number from left to right
for (let ch of s) {
let digit = getValue(ch);
res = res * b + digit;
}
return res;
}
// Driver Code
let s = "1A";
let b = 16;
console.log(decimalEquivalent(s, b));
Output
26