Created
October 28, 2014 14:46
-
-
Save blakewrege/d08b47df655d36e8aa95 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This program will convert unsigned int values for output in various bases | |
// including 2<= Base <= 16 and Base = 256 | |
// There will be a Convert(Value, Base) routine called with hard coded values from main | |
// The Base = 256 case will be handled separately. | |
// | |
#include <stdio.h> | |
void Convert(unsigned int,unsigned int); | |
void DoOtherConvert(unsigned int); | |
int main() | |
{ int Base, Value; | |
DoOtherConvert(123456789); | |
Convert(-1,16); | |
Convert(0xFACE,2); | |
} | |
// Convert will produce output with a printf() of a buffer prepared locally. | |
// The 'division algorithm' will be used to produce proper remainders (in backwards order) | |
// A table lookupwill be used to 'asciify' the remainders | |
void Convert( unsigned int Value, unsigned int Base) | |
{int BuffNdx; // values produced by the division algorithm | |
char *Table = "0123456789ABCDEF"; // for asciification of remainders | |
char OutputBuffer[80]; // characters will be placed here for a single printf("%s\n", ?) | |
if (Value == 0) printf("0"); | |
else | |
{ OutputBuffer[79] = (char) 0; // Put a null to prepare for %s | |
BuffNdx = 78; | |
while (Value != 0) | |
{ OutputBuffer[BuffNdx--] = Table[ Value % Base]; | |
Value = Value / Base; | |
} | |
printf("%s\n",&OutputBuffer[++BuffNdx]); | |
} | |
} | |
void DoOtherConvert(unsigned int Value) | |
{ | |
int Rem, BuffNdx; // values produced by the division algorithm | |
char *Table = "0123456789ABCDEF"; // for asciification of remainders | |
char OutputBuffer[80]; // characters will be placed here for a single printf("%s\n", ?) | |
OutputBuffer[79] = (char) 0; // Put a null to prepare for %s | |
BuffNdx = 78; | |
if (Value ==0) printf("0\n"); | |
else | |
while (Value != 0) | |
{ = Value % 256; | |
Value = Value / 256; | |
if (Rem == 0) OutputBuffer[BuffNdx--]= '0'; | |
else | |
{ while (Rem != 0) | |
{OutputBuffer[BuffNdx--] = Table[ Rem % 10]; | |
Rem = Rem / 10; | |
} | |
} | |
OutputBuffer[BuffNdx--] = '.'; | |
} | |
BuffNdx++; | |
printf("%s\n",&OutputBuffer[++BuffNdx]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment