Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

base n to base 10 conversion program.

Name: Jules 2012-03-20 16:56

hi guys, im here to ask for a favor that i know you people arent very fond of, but here it goes....AAAAAAAAAAAAAHHHHHHHHH!!!!!!! AAAAAHH!!! AHH!!....ok..the problem with this program is that its results are printed reverse. please provide hints..(or actual code) atoning for my mistakes. thank you. (basically, make my program work...please) thank you very much, and any help will be appreciated.

#include <conio.h>
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>


int converter(char number[], int base);
void main()
{
     int a,base1,base2,i=0;
     char rev[30],num[30];
     char arr[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  
     printf("\nInput your number : ");
     scanf("%s",num);
  
     printf("\nInput the base of the current number : ");
     scanf("%d",&base1);
    
     printf("\nInput the base of which the number should be converted to : ");
     scanf("%d",&base2);
   
     a = converter(num,base1);    
   
     while(a>0)
     {        
          rev[i] = arr[(a % base2)];
          a /= base2;
          i++;
     }
     rev[i]='\0';
     printf("\nConverted Number is : %s\n",rev);
  getch();
}
 
 
int converter(char number[], int base)
{
     int decValue =0,i=strlen(number)-1,val,j=0;
     while(i>=0)
     {
         
          switch(number[i])
          {
               case '0' : val =0; break;
               case '1' : val =1; break;
               case '2' : val =2; break;
               case '3' : val =3; break;
               case '4' : val =4; break;
               case '5' : val =5; break;
               case '6' : val =6; break;
               case '7' : val =7; break;
               case '8' : val =8; break;
               case '9' : val =9; break;
               case 'A' :
               case 'a' : val =10; break;
               case 'B' :
               case 'b' : val =11; break;
               case 'C' :
               case 'c' : val =12; break;
               case 'D' :
               case 'd' : val =13; break;
               case 'E' :
               case 'e' : val =14; break;
               case 'F' :
               case 'f' : val =15; break;
          }    
       
          decValue += ((int)pow((double)base,j)*val);    
          j++;        
          i--;
   
     }
     return decValue;
}

Name: Anonymous 2012-03-20 17:04

bump.

i believe its think piece of code, but its a completely antithetical result without it?

     while(a>0)
     {       
          rev[i] = arr[(a % base2)];
          a /= base2;
          i++;
     }

Name: Anonymous 2012-03-20 17:28

#include <conio.h>
#include "stdafx.h"
Crap.
#include <stdlib.h>
Is this used anywhere?
void main()
Nope.
printf("\nInput your number : ");
Line buffering. Also no error checking anywhere.
scanf("%s",num);
Buffer overflow.
rev[i] = arr[(a % base2)];
i++;
Wrong way.
getch();
Crap.
decValue += ((int)pow((double)base,j)*val);
What the fuck man. You don't need floating point. Also undefined behaviour when it gets out of the range of an int.

Also the style is completely shit, and you didn't use code tags.

Name: Anonymous 2012-03-20 17:41

Use strtol to convert strings to numbers. It's in the Standard Library. You're not writing your own scanf and printf, so why are you writing your own ad hoc, informally-specified, bug-ridden, slow implementation of half of strtol?

Name: Anonymous 2012-03-20 21:49

N=2
echo "ibase=$N;obase=10;110"|bc

You're welcome.

Name: Anonymous 2012-03-20 22:33

>>4
homework

Name: Anonymous 2012-03-20 23:17

>>1
Get a copy of K&R2, you dirty indian!

Name: Anonymous 2012-03-20 23:32

        switch(number[i])
          {
               case '0' : val =0; break;
               case '1' : val =1; break;
               case '2' : val =2; break;
               case '3' : val =3; break;
               case '4' : val =4; break;
               case '5' : val =5; break;
               case '6' : val =6; break;
               case '7' : val =7; break;
               case '8' : val =8; break;
               case '9' : val =9; break;
               case 'A' :
               case 'a' : val =10; break;
               case 'B' :
               case 'b' : val =11; break;
               case 'C' :
               case 'c' : val =12; break;
               case 'D' :
               case 'd' : val =13; break;
               case 'E' :
               case 'e' : val =14; break;
               case 'F' :
               case 'f' : val =15; break;
          }

. . . WHY
I think you mean
if (number[i] <= '9' && number[i] >= '0')
val = number[i] - 48; // '0' = 48
else if (number[i] > 'Z')
val = number[i] - 'A';
else
val = number[i] - 'a';
/* Under the assumption the string contains only valid hex characters */

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List