Skip to content

Instantly share code, notes, and snippets.

@SibTiger
Created September 18, 2018 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SibTiger/a14a3d97b1d3055bccc1402413fefe37 to your computer and use it in GitHub Desktop.
Save SibTiger/a14a3d97b1d3055bccc1402413fefe37 to your computer and use it in GitHub Desktop.
This program is designed to convert numbers from 0 to 255 in form of Binary and Hexadecimal formats.
// =====================================================
// Programmer: Nicholas Gautier
// Class: CS3013; Network Programming
// Assignment #: 1
// Due Date: 12. September. 2018
// Instructor: Dr. Zhao
// Description: This program was designed in mind of converting the first
// octet of an IPv4 address from Base 10 to Base 2 and
// Base 16. This program will convert from the starting
// point of '000' to '255'. Thus, this program will
// convert the integers automatically for the user -
// without requiring any user interaction from the user.
// NOTES: I used Visual Studio 2017 for this assignment, but
// it will run perfectly fine outside of this IDE.
// Also, I can not figure out how to get the spacing right
// using the %*, so I just grossly hardcoded in
// the spacing myself.
// Credits:
// Return Codes:
// 0 = Successful Operation
// !0 = See Operating System Documentation
// =====================================================
// Inclusions
// ================================
#include <stdio.h> // Required for Input and Output streams
#include <stdlib.h> // Required for the 'Exit' status
// ================================
// Macro Definitions
// ================================
#define _NAME_ "Simple IP Conversion" // Name of this program
#define _VERSION_ "1.0" // Version of this program
#define _VERSION_NAME_ "Mazedogs" // Version name of this program
#define _NUMLIMITSTART_ 0 // Number starting point; default is 0.
#define _NUMLIMITEND_ 255 // Number ending point; default is 255.
// ================================
// Function Prototypes
// ================================
void DrawHeader(); // Display the program information
void DrawAbout(); // Display what the program's purpose is and
// what it is doing.
void DrawInstructions(); // This function will merely provide the
// the program instructions to the end-user.
void PrintColumnNames(); // Display the column names to the end-user.
void PrintSeparator(); // Print a small horizontal line on to the
// terminal buffer.
void PrintBase10(int); // This function is designed to display
// the base 10 (decimal) number. Really,
// it is merely printing itself - because
// it is already in decimal form.
int PrintBase2(int); // This function is designed to display
// the base 2 form. This will convert
// The base 10 form to base 2 and display
// the results on the screen.
void PrintBase16(int); // This function is designed to display
// the base 16 form. This will convert
// the base 10 form to base 16 and
// display the results on the screen.
// ================================
// Main entry point of the program
int main(char argn, // Number of Arguments
char** argv) // Argument Values
{
// Draw the program's internal information to the user
DrawHeader();
// Draw the program's about information to the user
DrawAbout();
// Draw the program's instructions to the user
DrawInstructions();
// Provide extra spacing
printf("\n\n");
// Display the column names
PrintColumnNames();
// Display a small horizontal line
PrintSeparator();
// Immediately execute a scan from the starting (or lower) number and
// stop at (but still including) the ending (or higher) number.
for (int i = _NUMLIMITSTART_; i <= _NUMLIMITEND_; i++)
{
// Display the Base10
PrintBase10(i);
printf(" ");
printf("%08d", PrintBase2(i));
printf(" ");
PrintBase16(i);
printf(" ");
printf("\n");
} // for()
// Display a small horizontal line
PrintSeparator();
// Display the column names
PrintColumnNames();
// I only placed this here - because I am using the Windows environment.
// So in order to see the information, I placed this 'fgetc()' to halt
// the program until the user interacts by pressing the 'enter' key.
// With using this particular function, it is cross-platform friendly.
fgetc(stdin);
// Terminate the program with a success return code.
exit(EXIT_SUCCESS);
} // main()
// Print Base 10
// ====================================
// Documentation:
// This function is designed to display the integer (or decimal) value
// from the given parameter values.
// ====================================
// Arguments:
// num [int]
// The decimal value to be displayed on the screen.
// ====================================
void PrintBase10(int num)
{
// The number is already in Base 10, just display it!
printf("%03d", num, 10);
} // PrintBase10()
// Print Base 2
// ====================================
// Documentation:
// This function is designed to display the binary value
// from the given parameter values.
//
// I had help from these sources to make this recursive call possible.
// The Skeleton is my design, but to assure the return was accurate -
// I had to use some sources to help me through.
// - https://www.geeksforgeeks.org/decimal-binary-number-using-recursion/
// - https://www.wikihow.com/Convert-from-Decimal-to-Binary
// ====================================
// Arguments:
// num [int]
// The decimal value to be converted to Binary format.
// ====================================
int PrintBase2(int num)
{
if (num == 0)
// If num is '0', then simply return back '0'.
return 0;
else
// Recursive call to generate the binary form.
return (num % 2 + 10 * PrintBase2(num / 2));
} // PrintBase2()
// Print Base 16
// ====================================
// Documentation:
// This function is designed to display the hexadecimal value
// from the given parameter values.
// ====================================
// Arguments:
// num [int]
// The decimal value to be converted to Hexadecimal format.
// ====================================
void PrintBase16(int num)
{
// I used the 'X' switch - because WHY NOT!
// It's available and I used it and I loved it!
// http://www.cplusplus.com/reference/cstdio/printf/
printf("%02X", num);
} // PrintBase16()
// Instructions
// ====================================
// Documentation:
// Provide instructions to the user as to how-to use the program.
// ====================================
void DrawInstructions()
{
printf("This program is designed to perform one task only, there is no interaction necessary from the end-user.");
// Add some spacing for visibility
printf("\n\n");
} // DrawInstructions()
// Display Column Names
// ====================================
// Documentation:
// This function will display the column names to the user.
// This will help clarify the columns information.
// ====================================
void PrintColumnNames()
{
printf("Decimal Binary Hexadecimal");
printf("\n");
} // PrintColumnNames()
// Display Separator
// ====================================
// Documentation:
// This function will only display a small horizontal
// line on to the terminal buffer. Nothing fancy here.
// ====================================
void PrintSeparator()
{
printf("- - - - - - - - - - - - - - - - - -");
printf("\n");
} // PrintSeperator()
// Draw Header
// ====================================
// Documentation:
// This function is designed to provide a header to
// the top-most space on the terminal buffer.
// ====================================
void DrawHeader()
{
printf("%s - Version: %s\n", _NAME_, _VERSION_);
printf("%s\n", _VERSION_NAME_);
printf("--------------------------------------------\n\n");
} // DrawHeader()
// Draw About
// ====================================
// Documentation:
// This function is merely states what the program
// is going to do and it's purpose for why it exists.
// ====================================
void DrawAbout()
{
printf("This program will inspect a traditional IP address of '000.000.000.000', but only pick the first octet of such address. With that first octet, we will analyze what the address is in Binary and Hexadecimal. Thus, this program will present 0 through 255 and display what each number is in Binary and Hexadecimal.");
// Add some spacing for visibility
printf("\n\n");
} // DrawAbout()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment