473,799 Members | 3,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4023
Previously on the homework channel, jyc...@gmail.co m 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.co m 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.co m 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.c omwrites:

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*****@chromat ico.net
Mar 19 '07 #5
On 3¤ë19¤é, ¤U¤È11®É56¤À, mark_blue...@po box.com wrote:
Previously on the homework channel, jyc...@gmail.co m 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 theconversionca n 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",&n um,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
2093
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, so he will send us all on a conversion course. One of many reasons is that our code is littered with examples of: SUBROUTINE PRINT_ITEM(ITEM, ITEM_TYPE) IF (ITEM_TYPE .EQ. SQUARE) THEN CALL PRINT_SQUARE(ITEM)
1
1484
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 wrong.................THANK YOU....... ASSIGNMENT: Demonstrate to the instructor the successful completion of each program output. Have this sheet ready for instructor to sign. Hand to instructor
1
3726
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 attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
6
2501
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
2752
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 the sequence nos and it should be such that i can access it through the structure .plz help me .
11
2710
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> #include <string.h> #define LENGTH 20 int temp, m, n, i, r, base10, true;
3
1788
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 solve this problems. I cant tell u how much i will be gratefull if someone can help me. Please if anyone knows how to solve certain task reply to me.Thanks to all in advance. Tasks 1. Your are going to a restaurant where the tip is expected to be...
2
1477
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 here>: Inputs: Menu Selection = <put number here> Expected Outputs: <put expected output here> Currency_Type: I need to include a hierarchy and flow chart
1
1343
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 with the resulting number. The number entered will be either an integer or real number. The conversion process is from source base to decimal, then from decimal to the target base. For nonterminating conversions, I only need five places after...
0
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9546
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10491
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10247
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9079
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.