Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C Programming Language

help me to write a base conversion program


please~help me to write a program to convert the baseN(2-9) to base10

eg. 101(2)----->5(10)
      1211(3)------->X(10)
.....

P.S. i am really misunderstand the C programming, plz write the simple
statments as you can

Previously on the homework channel, jyc@gmail.com wrote:

> please~help me to write a program to convert the baseN(2-9) to base10

> eg. 101(2)----->5(10)
>       1211(3)------->X(10)
> .....

> P.S. i am really misunderstand the C programming, plz write the simple
> statments as you can

If you have a value "abc" representing a value in base X, then the
conversion can be expressed as this:-

     (((a * X) + b) * X) + c

So all you need to do (excluding handling invalid input, negative
values and the like), is to take a digit at a time from the left of
the input string and add its numeric value to a running total,
multiplying by the base each time round.

Try producing some code to do this, and get back to us.

For simplicity, pass the value to be converted and the base as
arguments to the program, so you don't need any input I/O - you can
just use argv[1] and argv[2], for example. You'll want something like
atoi() or strtol() to convert the base to an integer or long, but the
value to convert is probably easier handled as a string.

jyc@gmail.com wrote:
> please~help me to write a program to convert the baseN(2-9) to base10
> eg. 101(2)----->5(10)
>       1211(3)------->X(10)

Use the strtol() function to convert the input string into a
number and then printf() to print it out.

                                Regards, Jens
--
  \   Jens Thoms Toerring  ___      j@toerring.de
   \__________________________      http://toerring.de

jyc@gmail.com wrote:
> please~help me to write a program to convert the baseN(2-9) to base10

> eg. 101(2)----->5(10)
>       1211(3)------->X(10)
> .....

> P.S. i am really misunderstand the C programming, plz write the simple
> statments as you can

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
     char *s;
     unsigned b;

} entry;

void convert(entry e)
{
     unsigned long v;
     char *endp;
     if (e.b < 2 || e.b > 36) {
         printf("Refused to convert \"%s\" using illegal base %u\n\n",
                e.s, e.b);
         return;
     }
     v = strtoul(e.s, &endp, e.b);
     if (*endp) {
         printf("Attempt to convert \"%s\" (base %u) ends prematurely.\n"
                " Offending character is %c.\n", e.s, e.b, *endp);
     }
     printf("%s(%u)------>%lu(10)\n\n", e.s, e.b, v);

}

int main(void)
{
     entry stuff[] = {
         {"101", 2},
         {"1211", 3},
         {"12911", 8},
         {"klm", 16},
         {"0000", 1},
         {"abcz", 36},
         {"abcz", 37}
     };
     size_t nentries = sizeof stuff / sizeof *stuff, i;
     for (i = 0; i < nentries; i++)
         convert(stuff[i]);
     return 0;

}

101(2)------>5(10)

1211(3)------>49(10)

Attempt to convert "12911" (base 8) ends prematurely.
  Offending character is 9.
12911(8)------>10(10)

Attempt to convert "klm" (base 16) ends prematurely.
  Offending character is k.
klm(16)------>0(10)

Refused to convert "0000" using illegal base 1

abcz(36)------>481283(10)

Refused to convert "abcz" using illegal base 37

>>>>> "j" == jyck91  <jyc@gmail.com> writes:

    j> please~help me to write a program to convert the baseN(2-9) to
    j> base10 eg. 101(2)----->5(10) 1211(3)------->X(10) .....

    j> P.S. i am really misunderstand the C programming, plz write the
    j> simple statments as you can

Perhaps you should talk to your professor?  After all, you're paying
tuition so that he will teach you C programming (or your parents are,
or your government is).

Charlton

--
Charlton Wilbur
cwil@chromatico.net

On 3?19?, ??11?56?, mark_blue@pobox.com wrote:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define LENGTH 20
main()
{
         char num[LENGTH];
         int i, temp, base10, baseN, j;
         printf("Enter a number and baseN:");
         scanf("%s%d",&num,baseN);
         if( baseN >= 2){ // ensure the base is larger than 1
         for ( i=0; i<strlen(num)-1; i++) // check the length of
string, in fact , i don't know why i wirte it
*line14   temp = num[i]; // change the position of num
         num[i] = num[LENGTH]; // same
         num[LENGTH]= temp; // same
         for ( j=0; j<=strlen(num); i++) // actually i don't know what
i do
         base10 = 0;
         base10 = base10 + ((num[LENGTH] * baseN)+ num[i]+1);// find
the num of base10
         printf("base10 = %d\n",base10);
         system("PAUSE");

}
}

At line14, actually i want to eg..
1011(2)-----> 1*2^3+ 0*2^2+ 1*2^1+ 1*2^0---> 1*2^0+ 1*2^1+ 0*2^2+
1*2^3

and for this (((a * X) + b) * X) + c
i am not really understand how to write it

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc