In the last part of my C Video Tutorial, I covered how to convert from base 10 to base 2, base 8 and base 16 with C.
At the end of that tutorial, I provided a homework assignment for you to write code that converts from any base back to base 10. In this tutorial, I will walk through exactly how to do that.
You can find all of the code below the video. Please leave any questions you may have.
If you like videos like this, it helps to tell Google+ with a click here [googleplusone]
Code From the Video
CTutorial14.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> // Used for pow() #include <ctype.h> // Used for isalpha() // To get the commented code for this function see // http://www.newthinktank.com/2013/09/c-video-tutorial-13/ char * convertBase(unsigned int numberToConvert, int base) { char buffer[33]; char *pConvertedNumber; char allValues[] = "0123456789abcdef"; if(base < 2 || base > 16){ printf("Enter a Number Between 2 and 16\n"); exit(1); } pConvertedNumber = &buffer[sizeof(buffer)-1]; *pConvertedNumber = '\0'; do { int value = numberToConvert % base; pConvertedNumber = pConvertedNumber - 1; *pConvertedNumber = allValues[value]; numberToConvert /= base; } while(numberToConvert != 0); printf("%s", pConvertedNumber); return pConvertedNumber; } /* WORKS UNTIL LETTERS ARE USED int baseToDecimal(char* number, int baseFrom, int sizeOfNumber){ int result = 0; int toThePowerOf = 0; // Cycle through all letters in the array starting with the last one // We subtract 2 because indexing starts at 0 and to avoid the '\0' for ( int i = (sizeOfNumber - 2); i >= 0; --i ){ // Copying this style // ... + ( 4 * 8^1 ) + ( 5 * 8^0 ) result += (number[i] - '0') * pow(baseFrom, toThePowerOf); toThePowerOf++; } printf("%s in base %d equals %d in base 10\n", number, baseFrom, result); return result; } */ // WORKS WITH HEXADECIMALS int baseToDecimal(char* number, int baseFrom, int sizeOfNumber){ int result = 0; int toThePowerOf = 0; for ( int i = (sizeOfNumber - 2); i >= 0; --i ){ // Check if character in the array is a letter if(isalpha(number[i])){ // Convert the letter into the right character code // and subtract 87 to make 'a' = 10 along with other // letters int charCode = ((int)tolower(number[i])) - 87; // Copying this style // ... + ( 4 * 8^1 ) + ( 5 * 8^0 ) result += charCode * pow(baseFrom, toThePowerOf); } else { result += (number[i] - '0') * pow(baseFrom, toThePowerOf); } toThePowerOf++; } printf("%s in base %d equals %d in base 10\n", number, baseFrom, result); return result; } int main(void){ unsigned int numberOne = 60; // 111100 printf("%d in Base 2 = ", numberOne); convertBase(numberOne, 2); printf("\n%d in Base 8 = ", numberOne); convertBase(numberOne, 8); printf("\n%d in Base 16 = ", numberOne); convertBase(numberOne, 16); printf("\n"); // Work Out the Key Codes for Hexadecimals /* printf("a : %d\n", 'A'); printf("a : %d\n", 'b'); printf("a : %d\n", 'c'); printf("a : %d\n", 'd'); printf("a : %d\n", 'e'); printf("a : %d\n", 'f'); */ char numberToConvert[] = "3C"; baseToDecimal(numberToConvert, 16, sizeof(numberToConvert)); return 0; }
Leave a Reply