User Tools

Site Tools


user:rrichar8:portfolio:baseconvert

Project: DECIMAL TO DIFFERENT BASE CONVERSION PROJECT

A project for C++ by Rich Richardson during the Fall 2011 SEMESTER YEAR.

This project was begun on 10/7/2011 and completed on 10/23/2011.
I may still make a few minor adjustments to this program.
To run this program, download the file at the following link:

http://dl.dropbox.com/u/40618236/Conversion%20Program3.exe

Objectives

The purpose of this project is to complete a program that will take a decimal number and convert it into another base. In the spring semester I had completed a similar program on Small Basic in my Computer Essentials class. The point of this project is that I will try to take the Small Basic programming ideas and place them into a working C code. Since this is my first solo project, I hope to accomplish a good start towards learning and understanding C code. It will also be handy to have a small program to use to convert numbers to a different base. This is especially helpful in Networking.

Prerequisites

In order to successfully accomplish and perform this project, the listed resources were needed to be consulted:

  • The use of the program 'Code Blocks 10.05' to assemble, compile and test the project code. Easy to use, and compatible with Putty.
  • Help from Matt to get the program to run multiple times with the use of an extra 'WHILE' in the code.
  • Use of the PUTTY to insure that the program functions and compiles properly.

Background

The purpose of this project is to complete a program that will take a decimal number and convert it into another base.
I am attempting to take an old program that was written in Small Basic and rewrite it in C style programming.
This program is very useful if you need to convert from decimal to another base.
Very handy for networking applications when you may not have access to the internet for conversion tables.

If you would like to run the program. Click on the following link:
Save the file, and click on it to run the program.
http://dl.dropbox.com/u/40618236/Conversion%20Program3.exe

Here is a link to the original program in SMALL BASIC programming that I am converting:
http://dl.dropbox.com/u/40618236/Small%20Basic%20Base%20Conversion.txt

Scope

I am producing a small sized 'C program' that I am calling baseconvert that will convert a decimal number into another base. I believe that it will be a relatively simple program, since converting numbers to a different base is basically a division function. There will be a need to assign the LETTERS 'A to F' to use for Hex conversion. I want the program to run as long as you have numbers to convert, so I will have to give it a While statement in addition to the math program. Also I want to make sure that the program will only accept the proper numbers, which must be POSITIVE numbers greater than zero to convert to another base. Also the bass number must be greater than ONE. I plan on putting in a warning sound if a number is entered that is out of range. While I'm at it, I may just as well produce another program that will convert BINARY numbers into decimal. I will plan on using that as an experiment and see if I can also make that program produce an Octal number at the same time. Again, these programs will be handy for future computer and math classes.

Attributes

This project will make use of variables, arrays, functions, repetition and more.

  • Declaring and Initializing Variables to input and output numerical data.
  • DO, WHILE and IF loops for program function and error routines.
  • Array initialization and use in the program.
  • Use of a sentinel to end program.
  • Use of a warning sound within the print function when incorrect data is input.

Procedure

The actual steps taken to accomplish the project. Include images, code snippets, command-line excerpts; whatever is useful for intuitively communicating important information for accomplishing the project.
First step I took to accomplish this project was to assemble the SMALL BASIC code from a project that I had done for Computer Essentials in the Spring of 2011 semester to see if I could take that programming and translate it into C code.
But to my dismay, the coding was very different than C code. Here is part of the SMALL BASIC code as an example.

idx = 0
TextWindow.WriteLine("Enter a DECIMAL NUMBER to convert it to a different BASE ")
TextWindow.WriteLine("Your entry can not be a NEGATIVE NUMBER ")
dec=TextWindow.Read()

If (dec < -1) Then

TextWindow.WriteLine("Decimal Number MUST BE GREATER THAN -1 ") 
goto start

Endif

TextWindow.WriteLine("What BASE do you wish to convert it to ? ")
TextWindow.WriteLine("BASE NUMBER must be greater than 1 ")
TextWindow.WriteLine("ENTER YOUR NUMBER ")
base=TextWindow.Read()

While (base < 2)

TextWindow.WriteLine("Base MUST BE GREATER THAN 1 ")
TextWindow.Write("Re-Enter BASE NUMBER ")
base=TextWindow.Read()

EndWhile

As you can see, the SMALL BASIC syntax is totally different than C Code syntax.
As I proceeded to convert to C code, I realized that I would have to rework the format of the code.
I also decided that I would give the program the ability to convert decimals to HEX format,
which the original SMALL BASIC program could not accomplish.
I found that it would be necessary to have an initialized array to accomplish that task.

Code

/* Rich - October 2011 - C/C++ Programming
 * Base Conversion Program - A program to convert a decimal number to a different base
 *
 *
 * Compile with: gcc -o baseconvert baseconvert.c -lm
 * Execute with: ./baseconvert
 */
#include <stdio.h>
#include <math.h>
 
int main()
{
   char baseDigits[16] =
	 {'0', '1', '2', '3', '4', '5', '6', '7',
	  '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
 
   int convertedNumber[64];
   long int decimalNumber;
   int base, index=0;
 
   printf("BASE CONVERSION PROGRAM  \n\n");
 
   printf("You can convert a POSITIVE whole DECIMAL NUMBER into another BASE  \n\n");
 
   do 
{  index = 0;
 
/* First - get the number to convert */
    printf("Enter the decimal number that you want to convert to another base: \n\n");
 
    printf("The number must be a POSITIVE whole number greater than ZERO: ");
 
    scanf("%ld", & decimalNumber);
 
/* If number is less than 1, need to re-enter the number. Give warning BEEP */
   while ( decimalNumber < 1 ) {
 
   printf("\n\n\n");
   printf("The number is you entered is less than 1. Please re-enter your number: \a");
   index = 0;
 
	scanf("%ld", & decimalNumber);
}
 
   printf("\n\n\n");
 
/* Next - get the base to convert it to */
    printf("Enter the BASE you'd like to convert the number to: \n\n");
 
    printf("The BASE must be a POSITIVE whole number greater than ONE: ");
 
    scanf("%i", & base);
 
/* If number is less than 2, need to re-enter the base number - Give warning BEEP*/
    while ( base < 2 ) {
 
   printf("\n\n\n");
   printf("The BASE you entered must be greater than 1. Please re-enter the BASE: \a");
 
   scanf("%i", & base);
}
 
   printf("\n\n\n");
 
/* Next - convert to the indicated base */
   while (decimalNumber != 0)
{
	 convertedNumber[index] = decimalNumber % base;
	 decimalNumber = decimalNumber / base;
	 ++index;
}
 
/* Next - use loop to print the answer  */
    --index;
 
    printf("Decimal Number converted to the Base you chose is: ", "\n\n");
 
    for(  ; index>=0; index--)
{
      printf("%c", baseDigits[convertedNumber[index]]);
}
   index=0;
   printf("\n\n\n");
       printf("Would you like to convert another number ? \n");
       printf("ENTER 0 for YES, any other number to QUIT:");
       scanf("%ld", &decimalNumber);
 
   printf("\n\n\n");
 
}  while (decimalNumber == 0);
 
   printf("Thanks for using the BASE CONVERSION PROGRAM ");
 
   printf("\n\n\n");
 
return(0);
}

Execution

lab46:~/src/cprog$ nano
lab46:~/src/cprog$ gcc -o baseconvert baseconvert.c -lm
lab46:~/src/cprog$ ./baseconvert
BASE CONVERSION PROGRAM

You can convert a POSITIVE whole DECIMAL NUMBER into another BASE

Enter the decimal number that you want to convert to another base:

The number must be a POSITIVE whole number greater than ZERO: 1477



Enter the BASE you'd like to convert the number to:

The BASE must be a POSITIVE whole number greater than ONE: 2



Decimal Number converted to the Base you chose is: 10111000101


Would you like to convert another number ?
ENTER 0 for YES, any other number to QUIT:0



Enter the decimal number that you want to convert to another base:

The number must be a POSITIVE whole number greater than ZERO: 95158



Enter the BASE you'd like to convert the number to:

The BASE must be a POSITIVE whole number greater than ONE: 16



Decimal Number converted to the Base you chose is: 173B6


Would you like to convert another number ?
ENTER 0 for YES, any other number to QUIT:1

Thanks for using the BASE CONVERSION PROGRAM


lab46:~/src/cprog$

Reflection

Before beginning this program, I thought that it would be very easy to take a small basic program and translate it into C programming code to function. I was in for a big disappointment. The syntax is totally different between the two codes. After first getting the program to function, I was perplexed on why I couldn't get the program to repeat itself. I had tried numerous ways but I did get stymied. Finally Matt suggested that I use an extra WHILE loop with a sentinel to accomplish the task. I did get it to function properly after hours of trying different formats. I was also interested in using a warning BEEP if someone entered incorrect information and give them the ability to re-enter the information again. I was able to accomplish this with another WHILE loop and when printing the error message I could make it BEEP with the use of an \a within the print function. As I was near completion of the project, I thought that I might as well start on a BINARY to Decimal program to accompany this program. I am working on that now and will put it in the experiment section as I go through it step by step. The real discovery I made when doing this project is that SMALL BASIC is very different than C code. Also that C code has many more shortcuts to use to reduce the size of the code.
I also found that a program is never DONE…You can always make it better, smaller, faster and more functional…It's pretty much like writing an essay or a song, it's never actually finished. It's just finished for the time being, it can always be improved upon.

References

user/rrichar8/portfolio/baseconvert.txt · Last modified: 2011/10/31 15:45 by rrichar8