Reputation: 10226
I am trying to convert from any base to base 10. For an input of 010111 base 2 it gives me 1, and for 35 base 9 it gives me 18 which should be 38. Any suggestions?
#include<stdio.h>
#include<math.h>
#include<string.h>
#define LENGTH 6
double pow( double x, double power );
int main()
{
char input[LENGTH+1] ;
int base;
unsigned decValue ;
char values[] = "0123456789ABCDEFG";
int i;
puts( "Enter a value, and its base: " );
scanf( "%s", &input );
scanf( "%i", &base );
for( i = LENGTH-1 ; i >= 0; i-- )
{
int index = strchr(values, input[ i ] ) - values;
decValue += index * pow( base , i - LENGTH + 1 );
}
printf( "%s (base-%u) equals %u \n", input, base, decValue );
return 0;
}
Upvotes: 0
Views: 4426
Reputation: 43410
The thing that troubles me here the most is your mixture of floating point arithmetic into an integer problem. Not only is it less efficient, but when the result is converted back to an int, there's the possibility it might get rounded down or something silly. Hypothetical example:
double n = pow(2,3); //becomes 7.99999999999999999999999 for some silly reason
int in = n; //rounds down to 7
Although this might not even be happening in your case, be wary of integer <-> floating point conversions, and avoid them when they are unnecessary.
A better solution is to build up the power incrementally. If I were you, I'd iterate from beginning to end of the number. Pseudocode:
let s be the input string's digits;
let r be the radix;
let n be the resulting number;
n=0;
for (i=0; i<s.size(); i++) {
n *= radix;
n += s[i];
}
The concept is that you are picking up digits and shifting them into the number. E.g. 123 in decimal:
1
(shift 1 left, then add 2)
12
(shift 12 left, then add 3)
123
Shifting left, in the context of integers in a given base, is simply multiplying by the radix.
Upvotes: 3
Reputation: 122519
Couple things: don't take the address of input
(input
, an array, already degrades to the pointer you want; taking a pointer of it will result in a pointer to an array, not what you want); and you need to make sure not to overflow the input
buffer when reading into it.
If this is not homework, you should just use strtol()
:
#include <stdlib.h>
#include <stdio.h>
#define Str(x) #x
#define Xstr(x) Str(x)
int main()
{
char input[LENGTH+1] ;
int base;
long decValue;
char *endptr;
puts( "Enter a value, and its base: " );
scanf( "%" Xstr(LENGTH) "s", input ); /* reads at most LENGTH characters */
scanf( "%i", &base );
decValue = strtol( input, &endptr, base );
assert( *endptr == '\0' ); /* if there are any characters left, then string contained invalid characters */
printf( "%s (base-%u) equals %ld\n", input, base, decValue );
return 0;
}
Upvotes: 0
Reputation: 45164
My suggestion: Dont reinvent the wheel.
See strtoul function:
#include <stdlib.h>
#include <stdio.h>
int main(){
/* first arg: number in base n to convert to base 10
* third arg: base n
*/
long int b10= strtoul("111",NULL,2);
printf("%ld\n",b10);
return 0;
}
Upvotes: 3
Reputation: 75459
A few comments:
strchr()
can return NULL
. You should never assume that it won't, especially when you're feeding it direct user input.toupper()
, and then you can recognize both a
and A
as hexidecimal 10.main()
.If you're doing this as a learning exercise, ignore this paragraph, but you appear to essentially be rewriting the standard library function strtol()
, which converts a string to a long
for arbitrary bases. It's pretty nice, actually, and has some functionality that you could incorporate into your code if you wanted.
Upvotes: 1
Reputation: 882641
Since i < LENGTH
, i - LENGTH + 1
is negative and the pow
is therefore 0.
So, you should use pow( base , LENGTH - i - 1 )
-- that one's the biggie. Other big bugs: you should use strlen(input) wherever you're using LENGTH; and you should initialize decValue to 0. There may be others, I stopped looking after the first three (this IS after all California, land of the "Three Strikes, You're Out" law;-).
Upvotes: 3
Reputation: 14941
You're adding to decValue before you ever assign anything to it. I didn't look too closely at your logic but that stood out immediately. Depending on platform that may cause you issues.
Upvotes: 1