www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/08/22/22:30:47

Date: Mon, 22 Aug 1994 21:03:16 -0300
From: "Michael T. Smith" <aa529 AT cfn DOT cs DOT dal DOT ca>
Sender: "Michael T. Smith" <aa529 AT cfn DOT cs DOT dal DOT ca>
Reply-To: "Michael T. Smith" <aa529 AT cfn DOT cs DOT dal DOT ca>
Subject: More of my stupid programming mistakes?
To: djgpp AT sun DOT soe DOT clarkson DOT edu

I have been having more trouble with my conversion program. This time I 
am almost done and have just a few (but still major) errors to clear up. 
There are two of them:

1. Converting from Binary to hex, decimal and octal doesn't work for me. 
If I enter a relatively small number like 111000, it converts it to 
"111000 Dec", "111000 Octal" and "111000 Hex" (or something along those 
lines). If I enter any binary number that is much larger, it always 
converts it to something like "4267267 Dec", "377777777 Octal" and 
"FFFFFF Hex". It must be something to do with strtoul, which I don't have 
quite figured out.

2. The Conversion Table, listing Oct, Dec and Hex equivalents to 255 Dec, 
doesn't work right. The Hex numbers are right, but the others are wrong:
Oct Dec Hex
-----------
73170 334848 0
73170 334848 1
73170 334848 2 etc.

I have included the functions in question. Any help for this clueless 
newbie would be appreciated (why am I having so much trouble with C???!!! 
I know that there are supposed to be errors, but this seems to be an 
all-out attack!).

/* convtable() gives a listing of equivved Bin/Hex/Dec/Oct numbers. */
/* SUN//14/08/1994//1629+ */
/* Originally, I was going to put in Binary numbers too, but that would
   take far too long, because I wouldn't be able to do a nice quick loop.
*/

void convtable(void)
{
  unsigned int convindex;
  char hitkey[3];
  FILE *toprn;
  
  fpurge(stdout);
  ScreenClear();

  printf("Please turn PRN on and on-line, and then press ENTER...");
  fpurge(stdin);
  gets(hitkey);
  toprn = fopen("PRN","w");
  ScreenClear();
  fprintf(toprn, "Octal/Decimal/Hexadecimal Equivalents\n");
  fprintf(toprn, "Oct Dec Hex\n");
  fprintf(toprn, "-----------\n");
  
  for(convindex = 0; convindex <= 255; convindex++)
  {
    fprintf(toprn, "%o %u %X\n");
  }
}

/* binconv() is Binary to All conversion. It is not with the rest of the
   "connies" because it is a fairly late addition, 08/08/1994
*/
/* MON//08/08/1994//2030+ */

void binconv(void)
{
  char binary[33];
  char hitkey[3];
  char *other; /* I don't completely understand strtoul(), so I don't
                 know what this should be called!
              */
  
  fpurge(stdout);
  ScreenClear();
  printf("\nEnter unsigned binary number (up to 32 bits long): ");
  fpurge(stdin);
  gets(binary);
  printf("\n\n%s Bin = \n\n", binary);
  printf("%lu Dec\n", strtoul(binary, &other, 10));
  printf("%lX Hex\n", strtoul(binary, &other, 16));
  printf("%lo Oct\n", strtoul(binary, &other, 8));
  printf("\nPress ENTER to continue...");
  fpurge(stdin);
  gets(hitkey);
}


****************************************************************
* Michael Smith       * Dartmouth, NS (Canada)                 *
* aa529 AT cfn DOT cs DOT dal DOT ca * Chebucto FreeNet (902)494-8006         *
****************************************************************
* Words of Wisdom:                                             *
* Beware of programmers carrying screwdrivers.                 *
* MACINTOSH: Machine Always Crashes If Not The OS Hangs        *
****************************************************************



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019