473,395 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Mar 19 '07 #1
5 4005
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
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
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
>>>>"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
On 3¤ë19¤é, ¤U¤È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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Anthony Jones | last post by:
Just a bit of background: I'm one of a group of FORTRAN programmers, looking to switch to C++. We are trying to write a few simple examples to demonstrate the power of the language to our manager,...
1
by: bigcjr44 | last post by:
I have an assignment do tonite and I can not get it started. We are learning about recursion. This is my assignment, and this is what I have written, could someone please tell me what I am doing...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
11
by: jyck91 | last post by:
// Base Conversion // Aim: This program is to convert an inputted number // from base M into base N. Display the converted // number in base N. #include <stdio.h> #include <stdlib.h>...
3
by: Dew | last post by:
Hello, iam new to programming, and ive been given a task in my Uni to complete certain exercises but i found it very difficulty to solve them. I was wandering if there is someone who can help me...
2
by: gcook | last post by:
Hello, I need some help! I need to genreate a set of test inputs and expected results for the currency conversion program below. I also have to use the folowing format Test Case <put number...
1
by: anderson1373 | last post by:
I want to write a program that will prompt the user for a number, its base, and the base to which it should be converted. I want the program yo convert the number and display an appropriate comment...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.