I\'m trying to convert any number from any base M to another base N, what I have
ID: 3562060 • Letter: I
Question
I'm trying to convert any number from any base M to another base N, what I have done in my code is to convert the inserted number from base N to decimal then convert it from decimal to base M. My code works fine for any base less than 37, but it's not handling fractions. For instance, if the input has fractions like 1001.01 the conversion doesn't come out correctly. Can anyone help me out
My code:
#include <iostream>
int charToVal(char curChar)
{
char convArray[] =
{'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
for(int i=0;i<37; ++i)
{
if(curChar==convArray[i])
{
return i;
}
}
return -1;
}
char valToChar(int val)
{
char convArray[] =
{'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
if(val <37 && val>=0)
{
return convArray[val];
}
return ' ';
}
bool convertToDecimal(char * gdstr, int length, int inConvertor, int & decimal)
{
int factor = 1;
decimal=0;
// std::cout << length;
for(int i=length-1; i >=0; --i)
{
int curVal(charToVal(gdstr[i]));
decimal += factor*curVal;
factor*=inConvertor;
}
// std::cout << decimal << std::endl;
return true;
}
bool convertFromDecimal(int decimal, int outConvertor, char * outString, int & length)
{
int fact = 1;
length=0;
while (decimal > 0)
{
fact = decimal%outConvertor;
char curChar(valToChar(fact));
outString[length] = curChar;
decimal/=outConvertor;
length++;
}
return true;
}
int main()
{
char out[50];
int outLeng=0;
int decVal;
int inLeng;
std::cout << "Length of InFormat " ;
std::cin >> inLeng;
char any[inLeng];
for(int i = 0; i< inLeng; i++)
{std::cout << "In Format Num ";
std::cin >> any[i];}
int inType;
std::cout << "In Format Tyep" ;
std::cin >> inType;
int outtype;
std::cout << "Out Format Tyep" ;
std::cin >> outtype;
convertToDecimal(any, inLeng, inType, decVal);
convertFromDecimal(decVal, outtype, out, outLeng);
std::cout << std::endl << "out Type is :" << std::endl;
for(int i = outLeng-1; i >=0; --i)
{
std::cout << out[i];
}
std::cout << std::endl;
}
Explanation / Answer
This might be helpful.. Its giving correct ans in my case
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include
#define MINBASE 2u /* smallest base */
#define MAXBASE 36u /* largest base */
int main()
{
void displayValueAndBases(unsigned long num, int base, int newbase);
long getNumberInBaseN(unsigned int b);
int getBase(unsigned int minbase, unsigned int maxbase);
int base; /* initial base */
int newbase; /* final base*/
unsigned long num; /* initial value, in base 10 */
printf("Enter initial base: "); /* grab initial base (in base 10) */
if ((base = getBase(MINBASE, MAXBASE)) != -1)
{
printf("Enter target base: "); /* grab target base */
if ((newbase = getBase(MINBASE, MAXBASE)) != -1)
{
printf("Enter number to convert: "); /* grab number */
while ((num = getNumberInBaseN(base)) != -1)
{
displayValueAndBases(num, base, newbase);
printf("Enter another base %i number to convert: ", base);
}
putchar(' ');
}
}
return EXIT_SUCCESS;
}
void displayValueAndBases(unsigned long num, int base, int newbase)
{
void displayValueInBase(unsigned long v, unsigned int b);
displayValueInBase(num, base);
printf(" in base %i is ", base);
displayValueInBase(num, newbase);
printf(" in base %i. ", newbase);
}
/*
* Read in the base using getNumberInBaseN
*/
int getBase(unsigned int minbase, unsigned int maxbase)
{
long getNumberInBaseN(unsigned int b);
long base;
if ((base = getNumberInBaseN(10)) == -1)
printf("Error: nonnumeric base detected ");
else
{
if (base < minbase) /* set to -1 if too small */
{
printf("Error: Smallest base is %u. ", minbase);
base = -1;
}
if (base > maxbase) /* set to -1 if too large */
{
printf("Error: Largest base is %u. ", maxbase);
base = -1;
}
}
return (int) base; /* base is in range of int */
}
/*
* Read a base N number, one character at a time.
*/
long getNumberInBaseN(unsigned int base)
{
int toDecimal(int c, int b);
int c; /* next input character */
int value; /* decimal value of next char */
long sum; /* running total */
sum = -1;
if ((c = getchar()) != EOF) /* EOF is error */
{
if ((value = toDecimal(c, base)) != -1)
{ /* within number, compute sum */
sum = 0;
while (value != -1)
{ /* valid next digit in base */
sum = base * sum + value; /* update number */
c = getchar();
value = toDecimal(c, base);
}
}
if (c != ' ')
{
sum = -1; /* indicate error! */
while (c != ' ') /* skip rest of line */
c = getchar();
}
}
return sum;
}
/*
* Functions to convert digits to and from characters (ASCII ONLY).
* toDecimal - convert char to equivalent decimal number.
* toChar - convert decimal number to equivalent character.
*/
int toDecimal(int c, int base)
{
int value;
if (isdigit(c)) /* set value if the input is anything */
value = c - '0'; /* we know how to handle */
else
if (islower(c))
value = c - 'a' + 10;
else
if (isupper(c))
value = c - 'A' + 10;
else /* if not, it's an error */
value = -1;
if (value >= base)
value = -1;
return value;
}
int toChar(int value)
{
int c;
if (value < 10)
c = '0' + value;
else
c = 'A' + value - 10;
return c;
}
/*
* Display a value in a specified base between 2 and 36.
*/
void displayValueInBase(unsigned long v, unsigned int b)
{
int toChar(int value);
unsigned int k; /* digits needed in result */
unsigned long divisor; /* initially b^(# of digits - 1) */
if (v == 0) /* zero is the same in any base */
printf("0");
else
{
k = floor(log10(v)/log10(b)) + 1;
divisor = pow(b, k - 1); /* first divisor: b^(k - 1) */
/* Run through value, calculating and displaying the value of each
of the digits in the new base (left to right) */
while (divisor >= 1)
{
putchar(toChar((int)(v / divisor)));
v = v % divisor;
divisor = divisor / b;
}
}
}