help me to write a base conversion program

J

jyck91

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
 
M

mark_bluemel

Previously said:
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.
 
J

Jens Thoms Toerring

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
 
M

Martin Ambuhl

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);
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
 
C

Charlton Wilbur

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
 
J

jyck91

Previously said:
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 inbaseX, then theconversioncan 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 thebaseeach time round.

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

For simplicity, pass the value to be converted and thebaseas
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 thebaseto an integer or long, but the
value to convert is probably easier handled as a string.

#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; // change the position of num
num = 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+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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top