I have to design a program that converts any base from 3 - 36 to binary. The problem I am having is the argument passed into my function keeps telling me that the value is 50 when it is really 2. 2 = 50 0 =48 I cannot find the problem.
int convert( constchar ExValue[], int Base , int * InValue )
{
int i;
int count = 0;
int j = ExValue[0];
printf( "%i\n" , j );
for( i = 0 ;; i++ )
{
if ( isalnum ( ExValue[i] ))
{
count++;
printf( "%i\n" , ExValue[i] );
}
if ( ExValue[i] == '\0' )
{
printf( "%i\n" , ExValue[i] );
break;
}
}
i = 0;
int Base10Value = 0;
int Base10Count = count;
for( count ; count >= 0 ; i++ )
{
if ( isalnum ( ExValue[i] ))
{
int BaseToPower = 1 ;
for ( Base10Count ; ( Base10Count - 1 ) > 0 ; Base10Count-- )
{
BaseToPower *= Base;
printf( "%i\n" , BaseToPower );
}
printf( "%i\n" , ExValue[i] );
Base10Value += ( ExValue[i] * BaseToPower);
count--;
printf( "%i\n" , Base10Value );
}
if ( ExValue[i] == '\0' )
{
printf( "%s\n" , "Breaking" );
break;
}
}
printf( "%i\n" , Base10Value );
}
That is the code I have so far. Many statements are used for error checking and such they can be commented out or deleted. The driver function is pasted below
1 2 3 4 5 6 7 8
int main()
{
char A[] = "200";
int * Value;
convert ( A , 3 , Value );
}
I was also wondering if there is a faster way to convert to binary. I know there are quick ways to do so from Hexadecimal and Octal, but the fastest way ( I thought of off the top of my head ) that works for all bases including ( 13, 14, 15 ) was to convert to a decimal number then to a binary representation of that number.