#include #include #include #include #include int get_binary(); int get_hex(); int get_decimal(); void display(int); void display_binary(int); void display_hex(int); void display_decimal(int); void help(void); void print_binary_digit(int); void print_hex_digit(int); void main(void) { char c; int x; int done = 0; cout << "\nRadix Converter\n"; help(); while(!done) { cout << "Command: "; c = getch(); cout << c << "\n"; switch(c) { case 'b': cout << "Binary number: "; x = get_binary(); display(x); break; case 'h': cout << "Hex number: "; x = get_hex(); display(x); break; case 'd': cout << "Decimal number: "; x = get_decimal(); display(x); break; case '?': help(); break; case 'q': done = 1; break; default: help(); break; } } } void help(void) { cout << "\nEnter d for decimal\n" << " h for hex\n" << " b for binary\n" << " q to quit\n" << " ? for help\n\n"; } void display(int x) { cout << "\n\n"; display_hex(x); display_decimal(x); display_binary(x); cout << "\n"; } int ctoi(char c) { switch(c) { case '0': return(0); case '1': return(1); case '2': return(2); case '3': return(3); case '4': return(4); case '5': return(5); case '6': return(6); case '7': return(7); case '8': return(8); case '9': return(9); case 'a': case 'A': return(10); case 'b': case 'B': return(11); case 'c': case 'C': return(12); case 'd': case 'D': return(13); case 'e': case 'E': return(14); case 'f': case 'F': return(15); default: cout << "ctoi: error nonnumeric character\n"; break; } } int get_binary(void) { int bin = 0; char c; while(1) { c = getch(); if(c == '0' || c == '1') { cout << c; bin = bin*2 + ctoi(c); } else if(c == 13) { return(bin); } } } int get_decimal(void) { int dec = 0; char c; while(1) { c = getch(); if(isdigit(c)) { cout << c; dec = dec*10 + ctoi(c); } else if(c == 13){ return(dec); } } } int get_hex() { int hex = 0; char c; while(1) { c = getch(); if(isxdigit(c)) { cout << c; hex = hex*16 + ctoi(c); } else if(c == 13) { return(hex); } } } void display_binary(int x) { int i; int nonzero = 0; cout << "Binary: "; for(i = 31; i >= 0; i--) { nonzero += (x >> i) & 1; if(nonzero) print_binary_digit((x >> i) & 1); } if(x == 0) print_binary_digit(0); cout << "\n"; } void display_decimal(int x) { cout << "Decimal: " << x << "\n"; } void display_hex(int x) { int i; int nonzero = 0; cout << "Hex: "; for(i = 7; i >= 0; i--) { nonzero += (x >> (i * 4)) & 0xF; if(nonzero) print_hex_digit((x >> (i * 4)) & 0xF); } if(x == 0) print_hex_digit(0); cout << "\n"; } void print_binary_digit(int x) { switch(x) { case 0: cout << "0"; break; case 1: cout << "1"; break; default: cout << "Error, print_digit accepts 0-15 only\n"; exit(-1); break; } } void print_hex_digit(int x) { switch(x) { case 0: cout << "0"; break; case 1: cout << "1"; break; case 2: cout << "2"; break; case 3: cout << "3"; break; case 4: cout << "4"; break; case 5: cout << "5"; break; case 6: cout << "6"; break; case 7: cout << "7"; break; case 8: cout << "8"; break; case 9: cout << "9"; break; case 10: cout << "A"; break; case 11: cout << "B"; break; case 12: cout << "C"; break; case 13: cout << "D"; break; case 14: cout << "E"; break; case 15: cout << "F"; break; default: cout << "Error, print_hex_digit accepts 0-15 only\n"; exit(-1); break; } }