home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / BCDL.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  114 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*-----------------01-10-95 02:38pm-----------------
  4. --  BCD conversion routines, released to the public
  5. --  domain by Jeff Dunlop
  6. --------------------------------------------------*/
  7.  
  8. #include "sniptype.h"         /* For True_, False_    */
  9. #include "snipmath.h"
  10.  
  11. static int BCDLen = 8;
  12.  
  13. void SetBCDLen(int n)
  14. {
  15.       BCDLen = n;
  16. }
  17.  
  18. long BCDtoLong(char *BCDNum)
  19. {
  20.       int i, high, low;
  21.       Boolean_T neg;
  22.  
  23.       long total = 0;
  24.  
  25.       for (i = 0; i < BCDLen - 1; i++)
  26.       {
  27.             low    = 0x0f & BCDNum[i];
  28.             high   = (0xf0 & BCDNum[i]) >> 4;
  29.             total *= 10;
  30.             total += high;
  31.             total *= 10;
  32.             total += low;
  33.       }
  34.       high   = (BCDNum[BCDLen - 1] & 0xf0) >> 4;
  35.       low    = BCDNum[BCDLen - 1] & 0x0f;
  36.       total *= 10;
  37.       total += high;
  38.  
  39.       neg = (low == 0x0c) ? 1 : -1;
  40.  
  41.       return total * neg;
  42. }
  43.  
  44. void LongtoBCD(long num, char BCDNum[])
  45. {
  46.       int i, high, low;
  47.       Boolean_T neg = False_;
  48.  
  49.       if ( num < 0 )
  50.       {
  51.             neg = True_;
  52.             num = 0 - num;
  53.       }
  54.  
  55.       low  = neg ? 0x0d : 0x0c;
  56.       high = (int)(num % 10);
  57.       num /= 10;
  58.       BCDNum[BCDLen - 1] = (char)((high << 4) | low);
  59.  
  60.       for (i = BCDLen - 2; i >= 0; i--)
  61.       {
  62.             low  = (int)(num % 10);
  63.             num /= 10;
  64.             high = (int)(num % 10);
  65.             num /= 10;
  66.             BCDNum[i] = (char)((high << 4) | low);
  67.       }
  68. }
  69.  
  70. #ifdef TEST
  71.  
  72. #include <stdio.h>
  73.  
  74. void show_BCD(char c[]);
  75.  
  76. int main(void)
  77. {
  78.       long a = 12345678L,
  79.            b;
  80.       char c[10];
  81.  
  82.       SetBCDLen(10);
  83.  
  84.       LongtoBCD(a, c);
  85.       show_BCD(c);
  86.       b= BCDtoLong(c);
  87.       printf("Value is %ld\n", b);
  88.  
  89.       a = -a;
  90.       LongtoBCD(a, c);
  91.       show_BCD(c);
  92.       b= BCDtoLong(c);
  93.       printf("Value is %ld\n", b);
  94.  
  95.       return 0;
  96. }
  97.  
  98. void show_BCD(char c[])
  99. {
  100.       int i;
  101.  
  102.       printf("BCD: ");
  103.       for (i = 0; i < 10; ++i)
  104.       {
  105.             printf("[%d]", c[i] >> 4);
  106.             if (9 == i)
  107.                   printf("<sign>");
  108.             printf("[%d]", c[i] &0xf);
  109.       }
  110.       puts("");
  111. }
  112.  
  113. #endif /* TEST */
  114.