By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,697 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

help me to write a base conversion program

P: n/a
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

Mar 19 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.
Mar 19 '07 #2

P: n/a
jy****@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 ___ jt@toerring.de
\__________________________ http://toerring.de
Mar 19 '07 #3

P: n/a
jy****@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

Mar 19 '07 #4

P: n/a
>>>>"j" == jyck91 <jy****@gmail.comwrites:

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

jP.S. i am really misunderstand the C programming, plz write the
jsimple 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
cw*****@chromatico.net
Mar 19 '07 #5

P: n/a
On 3月19日, 下午11時56分, mark_blue...@pobox.com wrote:
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 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[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

Mar 20 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.