home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / packdec.zip / PDDEMO.C < prev    next >
C/C++ Source or Header  |  1994-09-01  |  3KB  |  92 lines

  1. /************************************************************************
  2.  
  3.     IBM Packed Decimal Conversion Demo Program
  4.     copyright (c) 1994 Iambic Software, all rights reserved
  5.  
  6. *************************************************************************/
  7. #include "pd.h"
  8. #include <stdio.h>
  9. #include <mem.h>
  10. static void HexDump ( unsigned char * source, unsigned char * hex, short len );
  11. void main(void)
  12. {
  13.     unsigned char p[6]= "\x00\x12\x34\x56\x78\x9d"; /* packed test numb */
  14.     unsigned char p2[8];                            /* target packed numb */
  15.     long lBin;                                      /* work number */
  16.     short int iRc;                                  /* return code */
  17.     unsigned char hex[17];                          /* hex version of numb */
  18.     unsigned char *FirstSig;                                                                /* pointer to sig byte */
  19.     unsigned char mask1[8] = " \x10\x10,\x10\x10\x10";       /* mask 1 */
  20.     unsigned char pnum1[3] = "\x23\x00\x0c";                                                /* pack number 1 */
  21.     unsigned char mask2[13] = "\x20\x10\x10\x10\x10,\x10\x10\x10.\x10\x10";
  22.     unsigned char pnum2[5] = "\x01\x53\x23\x72\x5c";
  23.     unsigned char mask3[16] = "$\x10,\x10\x10\x10,\x10\x10\x10.\x10\x10" "CR";
  24.     unsigned char work16[16];                                                               /* work for edit */
  25.     unsigned char pnum3[5] = "\x00\x00\x00\x23\x2d";
  26.     printf(" Packed Decimal in C Demo \n\n");
  27.     iRc=sPackedToBin( p, &lBin, sizeof(p));   /* convert to Binary */
  28.     if ( iRc )                           /* always check return code */
  29.     {
  30.         printf("Conversion failed rc=%d\n",iRc);
  31.         return;
  32.     }
  33.     HexDump( p, hex, sizeof(p));
  34.     printf("packed number = 0x%s\nbinary= %ld\n", hex, lBin);
  35.     sBinToPacked( p2, &lBin );                 /*  convert to Decimal */
  36.     HexDump( p2, hex, sizeof(p2));
  37.     printf("packed number from binary one = 0x%s\n", hex);
  38.     printf("\n Example of overflow\n\n");
  39.     lBin=3231231231l;             /* this constant is really negative */
  40.     sBinToPacked( p2, &lBin );    /* convert to packed */
  41.     HexDump( p2, hex, sizeof(p2));
  42.     printf("overflow number= %s\n", hex);
  43.     printf("\n\nDemo of bonus program:  edit and mark\n\n");
  44.     iRc = sEdMk( mask1, sizeof( mask1), pnum1, &FirstSig);
  45.     if ( iRc == 4 )
  46.     {
  47.         printf("Data execption, pnum1\n");
  48.         return;
  49.     }
  50.     printf(" 23000 edited is: %s\n", mask1);
  51.     iRc = sEdMk( mask2, sizeof( mask2), pnum2, &FirstSig);
  52.     if ( iRc == 4 )
  53.     {
  54.         printf("Data execption, pnum2\n");
  55.         return;
  56.     }
  57.     *FirstSig = '$';
  58.     printf(" 153237.25 edited is: %s\n", mask2);
  59.     memcpy(work16, mask3, sizeof(work16) );                         /* move mask to work */
  60.     iRc = sEdMk( work16, sizeof( mask3), pnum3, &FirstSig); /* edit number */
  61.     if ( iRc == 4 )
  62.     {
  63.         printf("Data execption, pnum3\n");
  64.         return;
  65.     }
  66.     printf(" -2.32 edited is: %s\n", work16);
  67. }
  68.  
  69. /* -----------------------------------------------------------------------
  70.  
  71.      debugging routine
  72.      display number in hex
  73.  
  74. */
  75.  
  76.  
  77. static void HexDump ( unsigned char * source, unsigned char * hex, short len )
  78. {
  79.    short iI;
  80.     /*  len is length of source, hex should be twice as long  plus 1*/
  81.    for ( iI = 1; iI <= len; iI++ )
  82.     {
  83.         sprintf( hex, "%02X", *source );
  84.         hex += 2;
  85.         source++;
  86.     }
  87.     *hex = 0;                     /* end string */
  88.  
  89. }
  90.  
  91.  
  92.