Convert a number from base A to base B
Given two positive integers A and B and a string S of size N, denoting a number in base A, the task is to convert the given string S from base A to base B.
Examples:
Input: S = “10B”, A = 16, B = 10
Output: 267
Explanation: 10B in hexadecimal (base =16) when converted to decimal (base =10) is 267.
Input: S = “10011”, A = 2, B = 8
Output: 23
Explanation: 10011 in binary (base =2) when converted to octal (base = 8) is 23.
Approach: Number systemsis the technique to represent numbers in the computer system architecture. The computer architecture supports the following number systems:
- Binary Number System (Base 2): The binary number system only consists of two digits, 0s and 1s. The base of this number system is 2.
- Octal Number System (Base 8): The octal number system consists of 8 digits ranging from 0 to 7.
- Decimal Number System (Base 10): The decimal number system consists of 10 digits ranging from 0 to 9.
- Hexadecimal Number System (Base 16): The hexadecimal number system consists of 16 digits with 0 to 9 digits and alphabets A to F. It is also known as alphanumeric code as it consists of both number and alphabets.
To convert a number from base A to base B, the idea is to first convert it to its decimal representation and then convert the decimal number to base B.
Conversion from any base to Decimal: The decimal equivalent of the number “str” in base “base” is equal to 1 * str[len – 1] + base * str[len – 2] + (base)2 * str[len – 3] + …
Conversion from Decimal to any base:
The decimal number “inputNum” can be converted to a number on base “base” by repeatedly dividing inputNum by base and store the remainder. Finally, reverse the obtained string to get the desired result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int val( char c)
{
if (c >= '0' && c <= '9' )
return ( int )c - '0' ;
else
return ( int )c - 'A' + 10;
}
int toDeci(string str, int base)
{
int len = str.size();
int power = 1;
int num = 0;
for ( int 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;
}
char reVal( int num)
{
if (num >= 0 && num <= 9)
return ( char )(num + '0' );
else
return ( char )(num - 10 + 'A' );
}
string fromDeci( int base, int inputNum)
{
string res = "" ;
while (inputNum > 0) {
res += reVal(inputNum % base);
inputNum /= base;
}
reverse(res.begin(), res.end());
return res;
}
void convertBase(string s, int a, int b)
{
int num = toDeci(s, a);
string ans = fromDeci(b, num);
cout << ans;
}
int main()
{
string s = "10B" ;
int a = 16, b = 10;
convertBase(s, a, b);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int val( char c)
{
if (c >= '0' && c <= '9' )
return ( int )c - '0' ;
else
return ( int )c - 'A' + 10 ;
}
static int toDeci(String str, int base)
{
int len = str.length();
int power = 1 ;
int num = 0 ;
for ( int i = len - 1 ; i >= 0 ; i--)
{
if (val(str.charAt(i)) >= base)
{
System.out.printf( "Invalid Number" );
return - 1 ;
}
num += val(str.charAt(i)) * power;
power = power * base;
}
return num;
}
static char reVal( int num)
{
if (num >= 0 && num <= 9 )
return ( char )(num + '0' );
else
return ( char )(num - 10 + 'A' );
}
static String fromDeci( int base, int inputNum)
{
String res = "" ;
while (inputNum > 0 )
{
res += reVal(inputNum % base);
inputNum /= base;
}
res = reverse(res);
return res;
}
static void convertBase(String s, int a, int b)
{
int num = toDeci(s, a);
String ans = fromDeci(b, num);
System.out.print(ans);
}
static String reverse(String input)
{
char [] a = input.toCharArray();
int l, r = a.length - 1 ;
for (l = 0 ; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.valueOf(a);
}
public static void main(String[] args)
{
String s = "10B" ;
int a = 16 , b = 10 ;
convertBase(s, a, b);
}
}
|
Python3
def val(c):
if (c > = '0' and c < = '9' ):
return ord (c) - 48
else :
return ord (c) - 65 + 10
def toDeci(strr, base):
lenn = len (strr)
power = 1
num = 0
for i in range (lenn - 1 , - 1 , - 1 ):
if (val(strr[i]) > = base):
print ( "Invalid Number" )
return - 1
num + = val(strr[i]) * power
power = power * base
return num
def reVal(num):
if (num > = 0 and num < = 9 ):
return chr (num + 48 )
else :
return chr (num - 10 + 65 )
def fromDeci(base, inputNum):
res = ""
while (inputNum > 0 ):
res + = reVal(inputNum % base)
inputNum / / = base
res = res[:: - 1 ]
return res
def convertBase(s, a, b):
num = toDeci(s, a)
ans = fromDeci(b, num)
print (ans)
s = "10B"
a = 16
b = 10
convertBase(s, a, b)
|
C#
using System;
public class GFG{
static int val( char c)
{
if (c >= '0' && c <= '9' )
return ( int )c - '0' ;
else
return ( int )c - 'A' + 10;
}
static int toDeci( string str, int basse)
{
int len = str.Length;
int power = 1;
int num = 0;
for ( int i = len - 1; i >= 0; i--)
{
if (val(str[i]) >= basse)
{
Console.Write( "Invalid Number" );
return -1;
}
num += val(str[i]) * power;
power = power * basse;
}
return num;
}
static char reVal( int num)
{
if (num >= 0 && num <= 9)
return ( char )(num + '0' );
else
return ( char )(num - 10 + 'A' );
}
static string fromDeci( int basse, int inputNum)
{
string res = "" ;
while (inputNum > 0)
{
res += reVal(inputNum % basse);
inputNum /= basse;
}
res = reverse(res);
return res;
}
static void convertbasse( string s, int a, int b)
{
int num = toDeci(s, a);
string ans = fromDeci(b, num);
Console.Write(ans);
}
static string reverse( string input)
{
char [] a = input.ToCharArray();
int l, r = a.Length - 1;
for (l = 0; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return new string (a);
}
static public void Main (){
string s = "10B" ;
int a = 16, b = 10;
convertbasse(s, a, b);
}
}
|
Javascript
<script>
function val(c)
{
if (c >= '0' && c <= '9' )
return c.charCodeAt(0) - 48;
else
return c.charCodeAt(0) - 65 + 10;
}
function toDeci(str, base)
{
var len = str.length;
var power = 1;
var num = 0;
for ( var i = len - 1; i >= 0; i--) {
if (val(str[i]) >= base) {
document.write( "Invalid Number" );
return -1;
}
num += val(str[i]) * power;
power = power * base;
}
return num;
}
function reVal(num)
{
if (num >= 0 && num <= 9)
return String.fromCharCode(num + 48);
else
return String.fromCharCode(num - 10 + 65);
}
function fromDeci(base, inputNum)
{
var res = "" ;
while (inputNum > 0) {
res += reVal(inputNum % base);
inputNum = Math.floor(inputNum/base);
}
res = res.split( "" ).reverse().join( "" );
return res;
}
function convertBase(s, a, b)
{
var num = toDeci(s, a);
var ans = fromDeci(b, num);
document.write(ans);
}
var s = "10B" ;
var a = 16
var b = 10;
convertBase(s, a, b);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
12 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...