home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20243 < prev    next >
Encoding:
Text File  |  1993-01-27  |  2.3 KB  |  65 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!paladin.american.edu!howland.reston.ans.net!usc!sdd.hp.com!decwrl!pacbell.com!UB.com!pippen.ub.com!rfries
  3. From: rfries@sceng.ub.com (Robert Fries)
  4. Subject: Re: Problem with BC 3.1 vs. Gnu C
  5. Message-ID: <rfries.180@sceng.ub.com>
  6. Sender: news@pippen.ub.com (The Daily News)
  7. Nntp-Posting-Host: 128.203.1.151
  8. Organization: Ungermann Bass
  9. References: <1k3n0vINNg09@boa.cis.ohio-state.edu>
  10. Date: Tue, 26 Jan 1993 16:22:10 GMT
  11. Lines: 52
  12.  
  13. In article <1k3n0vINNg09@boa.cis.ohio-state.edu> imes@cis.ohio-state.edu (jeffrey imes) writes:
  14.  
  15.  
  16. >Here's the situation:
  17. >The program is very simple.  It just reads in decimal (integer) numbers from 1
  18. >to max size of type (using unsigned int in UNIX, unsigned long in DOS...should
  19. >produce same range) and converts them to a string of "hexadecimal" characters
  20. >(I quote that since they're not actual hex, just actual ASCII characters
  21. >re would look like in hex) look like in hex).  When run under UNIX, it will
  22. >successfully read in any number from 1 to 2,147,483,647 and convert it to hex.
  23. >When run under DOS, at every multiple of 2^16 (65536) I think overflow occurs
  24. >and it resets the number back to zero.  So 1=1, 10=A, 16=10,...,65535=7FFF,
  25. >and then 65536 won't work.  Then, 65537=1,65538=2,...
  26.  
  27. >Here's the code:
  28.  
  29. >.
  30. >.
  31. >.                 or long for BC
  32. >main()            |
  33. >{                 V
  34. >       unsigned int    dec_num,temp,.....;
  35. >       char    hex_num[10],hex,....;
  36.  
  37. >       printf("input a number 1-->2^32-1: ");
  38. >       scanf("%d",&dec_num);
  39. >       if ((dec_num < 1) || (dec_num > 2147483647))
  40. >               printf("Invalid range..try again...");
  41. >       if...
  42. >.
  43. >.
  44. >.
  45.  
  46. >and on and on.  Now, shouldn't the first scanf see that 65536 is a legal
  47. >number and let it go?  Then later on, why would it reset numbers greater than
  48. >65536 to 0?
  49.  
  50. I think this is caused by using %d in your call to scanf. Using BC3.1
  51. this will get a 16 bit number. I'm pretty sure you need to use something like
  52. %ld to get a long.
  53.  
  54. Robert
  55.  
  56.  
  57. //////////////////////////////////////////////////////////////////////////
  58. Robert Fries
  59. Ungermann-Bass Inc.
  60.  
  61. DISCLAIMER:
  62.     Opinions contained herein are my own, and are not necessarily those
  63.     of Ungermann-Bass.
  64. //////////////////////////////////////////////////////////////////////////
  65.