/*************************************************************************** * Copyright (C) 2004 by Derek Rabideau * * xsynackx@hotmail.com * * * * numcv-0.2.5.6a.c * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include /* for input output and most everything else needed */ #include /* for pow() functionality from math library */ #include #include int opt1 (char *arg1); int opt3 (char *arg1, char *arg2, char *arg3); void d_to_o (char *num); void d_to_x (char *num); void d_to_a (char *num); void o_to_d (char *num); void o_to_x (char *num); void o_to_a (char *num); void x_to_d (char *num); void x_to_o (char *num); void x_to_a (char *num); void a_to_d (char *num); void a_to_o (char *num); void a_to_x (char *num); void a_to_b (char *num); void d_to_b (char *num); void o_to_b (char *num); void x_to_b (char *num); int bincon (int num); void version (); int main (int argc, char **argv) { int todo; /* assigned to value opt3 returns. tells program what to do */ if (argc <= 1 || argc == 3 || argc >= 5) /* if less than 1, exactly 2, or more than 3 options, reject */ printf ("Error: Wrong number of options.\n"); else if (argc == 2) /* else if exactly 1 option continue */ { todo = opt1 (argv[1]); switch (todo) { case 1: version (); break; default: printf ("Error: Action not implemented.\n"); break; } } else if (argc == 4) /* if exactlly 3 options, continue */ { todo = opt3 (argv[1], argv[2], argv[3]); /* sets todo to return of opt3 */ switch (todo) { case 1: d_to_o (argv[2]); break; case 2: d_to_x (argv[2]); break; case 3: d_to_a (argv[2]); break; case 4: o_to_d (argv[2]); break; case 5: o_to_x (argv[2]); break; case 6: o_to_a (argv[2]); break; case 7: x_to_d (argv[2]); break; case 8: x_to_o (argv[2]); break; case 9: x_to_a (argv[2]); break; case 10: a_to_d (argv[2]); break; case 11: a_to_o (argv[2]); break; case 12: a_to_x (argv[2]); break; case 13: d_to_b (argv[2]); break; case 14: o_to_b (argv[2]); break; case 15: x_to_b (argv[2]); break; case 16: a_to_b (argv[2]); break; default: printf ("Action not implemented.\n"); break; } } return 0; } /*accepts 1 arg from program invocation. returns value to be used in calling function in main*/ int opt1 (char *arg1) { /*if arg1 equals foo, return 1 */ if (!strcmp (arg1, "--version") || !strcmp (arg1, "-v")) return 1; else return 0; } /* opt3: accepts 3 args from program invocation. */ /* returns value to be used for calling respective function */ int opt3 (char *arg1, char *arg2, char *arg3) { /* format: if arg1 is equal to foo, AND arg3 is equal to goo, return number of action. */ if (!strcmp (arg1, "-d") && !strcmp (arg3, "-o")) return 1; else if (!strcmp (arg1, "-d") && !strcmp (arg3, "-x")) return 2; else if (!strcmp (arg1, "-d") && !strcmp (arg3, "-a")) return 3; else if (!strcmp (arg1, "-o") && !strcmp (arg3, "-d")) return 4; else if (!strcmp (arg1, "-o") && !strcmp (arg3, "-x")) return 5; else if (!strcmp (arg1, "-o") && !strcmp (arg3, "-a")) return 6; else if (!strcmp (arg1, "-x") && !strcmp (arg3, "-d")) return 7; else if (!strcmp (arg1, "-x") && !strcmp (arg3, "-o")) return 8; else if (!strcmp (arg1, "-x") && !strcmp (arg3, "-a")) return 9; else if (!strcmp (arg1, "-a") && !strcmp (arg3, "-d")) return 10; else if (!strcmp (arg1, "-a") && !strcmp (arg3, "-o")) return 11; else if (!strcmp (arg1, "-a") && !strcmp (arg3, "-x")) return 12; else if (!strcmp (arg1, "-d") && !strcmp (arg3, "-b")) return 13; else if (!strcmp (arg1, "-o") && !strcmp (arg3, "-b")) return 14; else if (!strcmp (arg1, "-x") && !strcmp (arg3, "-b")) return 15; else if (!strcmp (arg1, "-a") && !strcmp (arg3, "-b")) return 16; else return 0; } void d_to_o (char *num) { char *pEnd; char *szInput = num; long dec; dec = strtol (szInput, &pEnd, 0); printf ("The decimal number %ld as an octal number is %#lo.\n", dec, dec); } void d_to_x (char *num) { char *pEnd; char *szInput = num; long dec; dec = strtol (szInput, &pEnd, 0); printf ("The decimal number %ld as a hexadecimal number is %#lx.\n", dec, dec); } void d_to_a (char *num) { int dec = atoi (num); if (dec < 0 || dec > 127) printf ("The decimal number %d does not correlate to any ASCII character.\n", dec); else if (dec < 14 || dec == 127) printf ("The decimal number %d as an ASCII character is a nonprintable control character.\n", dec); else printf ("The decimal number %d as an ASCII character is \'%c\'.\n", dec, dec); } void o_to_d (char *num) { char *pEnd; char *szInput = num; long oct; oct = strtol (szInput, &pEnd, 8); printf ("The octal number %#lo as a decimal number is %ld.\n", oct, oct); } void o_to_x (char *num) { char *pEnd; char *szInput = num; long oct; oct = strtol (szInput, &pEnd, 8); printf ("The octal number %#lo as a hexadecimal number is %#lx.\n", oct, oct); } void o_to_a (char *num) { char *pEnd; char *szInput = num; long oct; oct = strtol (szInput, &pEnd, 8); if (oct < 0 || oct > 127) printf ("The octal number %#lo does not correlate to any ASCII character.\n", oct); else if (oct < 32 || oct == 127) printf ("The octal number %#lo as an ASCII character is a nonprintable control character.\n", oct); else printf ("The octal number %#lo as an ASCII character is \'%lc\'.\n", oct, (int)oct); } void x_to_d (char *num) { char *pEnd; char *szInput = num; long hex; hex = strtol (szInput, &pEnd, 16); printf ("The hexadecimal number %#lx as a decimal number is %ld.\n", hex, hex); } void x_to_o (char *num) { char *pEnd; char *szInput = num; long hex; hex = strtol (szInput, &pEnd, 16); printf ("The hexadecimal number %#lx as an octal number is %#lo.\n", hex, hex); } void x_to_a (char *num) { char *pEnd; char *szInput = num; long hex; hex = strtol (szInput, &pEnd, 16); if (hex < 0 || hex > 127) printf ("The hexadecimal number %#lx does not correlate to any ASCII character.\n", hex); else if (hex < 14 || hex == 127) printf ("The hexadecimal number %#lx as an ASCII character is a nonprintable control character.\n", hex); else printf ("The hexadecimal number %#lx as an ASCII character is \'%c\'.\n", hex, (int)hex); } void a_to_d (char *num) { if (strlen (num) < 2) printf ("The ASCII character \'%c\' as a decimal number is %d.\n", num[0], num[0]); else printf ("Error: You entered more than one ASCII character.\n"); } void a_to_o (char *num) { if (strlen (num) < 2) printf ("The ASCII character \'%c\' as an octal number is %#o.\n", num[0], num[0]); else printf ("Error: You entered more than one ASCII character.\n"); } void a_to_x (char *num) { if (strlen (num) < 2) printf ("The ASCII character \'%c\' as a hexadecimal number is %#x.\n", num[0], num[0]); else printf ("Error: You entered more than one ASCII character.\n"); } void a_to_b (char *num) { if ((strlen (num)) < 2) { printf ("The ASCII character \'%c\' as a binary number is ", num[0]); bincon (num[0]); } else printf ("Error: You entered more than one ASCII character.\n"); } void d_to_b (char *num) { char *pEnd; char *szInput = num; long dec; dec = strtol (szInput, &pEnd, 0); printf ("The decimal number %ld as a binary number is ", dec); bincon (dec); } void o_to_b (char *num) { char *pEnd; char *szInput = num; long oct; oct = strtol (szInput, &pEnd, 8); printf ("The octal number %#lo as a binary number is ", oct); bincon (oct); } void x_to_b (char *num) { char *pEnd; char *szInput = num; long hex; hex = strtol (szInput, &pEnd, 16); printf ("The hexadecimal number %#lx as a binary number is ", hex); bincon (hex); } /*bincon - converts number to binary. accepts dec, oct, hex, or ascii as num, prints out binary equivalent, returns 0 if all goes well */ int bincon (int num) { int flag; int exp = 0; int prod = 0; if (num == 0) { printf ("0.\n"); return 0; } if ((num % 2) == 0) flag = 0; else flag = 1; while (prod < num) { prod = pow (2, exp); exp++; } if (prod == num) { printf ("1"); exp--; while (exp > 0) { printf ("0"); exp--; } printf (".\n"); return 0; } prod = prod / 2; while (num > 1) { num = num - prod; printf ("1"); prod = prod / 2; while (prod > num) { printf ("0"); prod = prod / 2; } } if (flag == 1) printf ("1"); printf (".\n"); return 0; } /*void function. accepts no values, returns none. prints program ABOUT info.*/ void version () { printf ("***************************************************************************\n"); printf ("* Copyright (C) 2004 by Derek Rabideau *\n"); printf ("* xsynackx@hotmail.com *\n"); printf ("* *\n"); printf ("* numcv-0.2.5.6a *\n"); printf ("* *\n"); printf ("* This program is free software; you can redistribute it and/or modify *\n"); printf ("* it under the terms of the GNU General Public License as published by *\n"); printf ("* the Free Software Foundation; either version 2 of the License, or *\n"); printf ("* (at your option) any later version. *\n"); printf ("* *\n"); printf ("* This program is distributed in the hope that it will be useful, *\n"); printf ("* but WITHOUT ANY WARRANTY; without even the implied warranty of *\n"); printf ("* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *\n"); printf ("* GNU General Public License for more details. *\n"); printf ("* *\n"); printf ("* You should have received a copy of the GNU General Public License *\n"); printf ("* along with this program; if not, write to the *\n"); printf ("* Free Software Foundation, Inc., *\n"); printf ("* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *\n"); printf ("***************************************************************************\n"); }