Skip to content

Instantly share code, notes, and snippets.

@joncarlmatthews
Last active August 29, 2015 14:19
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 joncarlmatthews/3b50ed90a510cb62a1a5 to your computer and use it in GitHub Desktop.
Save joncarlmatthews/3b50ed90a510cb62a1a5 to your computer and use it in GitHub Desktop.
//
// main.c
// Small Base Conversion Program
//
// Created by Jon Carl Matthews on 23/04/2015.
// Copyright (c) 2015 Jon Carl Matthews. All rights reserved.
//
#include <stdio.h>
#include <math.h>
int main(int argc, const char * argv[])
{
unsigned short int finished = 0;
do {
// Constant to hold base digits up to and including base 16.
const char baseDigits[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
// 8 byte int to test user input. However we'll only store a number that can
// fit into a 4 byte unsigned int (0 - 4,294,967,295).
// Knowing the size of the number allows us to calculate the number of digits
// needed to store a converted number of the maximum size into the smallest
// base. This being 32.
signed long int numberToConvert;
// 33 element array to hold the result in the correct order.
// (32 keys to hold the maximum amount of digits. 1 key to hold the null char)
char converted[33];
// Temporary array with the same properties as the converted array to hold
// each converted char in their natural order (reverse).
char convertedTmp[sizeof(converted)];
// Set each value of both arrays to the null char (0x0).
for (unsigned short int u = 0; u < (sizeof(convertedTmp)); u++) {
*(converted+u) = '\0';
*(convertedTmp+u) = '\0';
}
// 2 byte int to store the target base.
unsigned short int targetBase;
do{
// Fetch the base 10 number to convert.
printf("Enter a base 10 nunber to convert: ");
scanf("%li", &numberToConvert);
// Check the number is within range.
if (numberToConvert < 1){
printf("Error. Must be a positive number\n");
}
if (numberToConvert > 4294967295){
printf("Error. Number must be no bigger than 4,294,967,295\n");
}
}while ( (numberToConvert < 1) || (numberToConvert > 4294967295) );
do{
// Fetch the base to convert the number into.
printf("Enter base to convert number into: ");
scanf("%hi", &targetBase);
// Check the base is within range.
if ( (targetBase < 2) || (targetBase > 16) ){
printf("Error. Base must be between 2 and 16\n");
}
}while ( (targetBase < 2) || (targetBase > 16) );
/*
Formula
10 = base 10 number to convert.
2 = Target base.
10 % 2 = 0
10 / 2 = 5
5 % 2 = 1
5 / 2 = 2.5
2 % 2 = 0
2 / 2 = 1
1 % 2 = 1
1 / 2 = 0.5
*/
// Variable to hold the result of the division.
// signed long int so the compiler doesnt complain
// about implicit conversion during calculation.
signed long int dividedRes;
// Variable to hold the result of the modulus
// calculation.
unsigned int modulusRes;
// Variable to hold the decrementing numerator.
signed long int numerator = numberToConvert;
unsigned short int i = 0;
do{
modulusRes = (numerator % targetBase);
dividedRes = (numerator / targetBase);
numerator = dividedRes;
// Add result to rempory array.
convertedTmp[i] = baseDigits[modulusRes];
i++;
}while (dividedRes > 0);
// Loop through the tempory array in reverse order and add each element
// to our converted array.
i = 0;
for (signed short int x = 32; x >= 0; --x) {
if (*(convertedTmp+x) != '\0') {
converted[i] = *(convertedTmp+x);
i++;
}
}
printf("Result is: %s\n\n", converted);
}while (0 == finished);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment