home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / midifil2.zip / mftovlq / mftovlq.c next >
Text File  |  1993-10-05  |  2KB  |  85 lines

  1. /* ===========================================================================
  2.  * mfvlq.c
  3.  *
  4.  * Converts ULONGs entered by the user into the variable length quantity equivalent and
  5.  * displays that.
  6.  * =========================================================================
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <os2.h>
  11.  
  12. #include "midifile.h"
  13.  
  14.  
  15.  
  16. /* *************************** hex2bin() ******************************
  17.  * Convert an ascii hex numeric char (ie, '0' to '9', 'A' to 'F') to its binary equivalent.
  18.  ******************************************************************* */
  19.  
  20. USHORT hex2bin(UCHAR c)
  21. {
  22.    if(c < 0x3A)
  23.      return (c - 48);
  24.    else
  25.      return (( c & 0xDF) - 55);
  26. }
  27.  
  28.  
  29.  
  30. /* ********************************** main() ***********************************
  31.  * Program entry point. Calls the MIDIFILE.DLL function MidiVLQToLong.
  32.  *************************************************************************** */
  33.  
  34. main(int argc, char *argv[], char *envp[])
  35. {
  36.     register USHORT i;
  37.     ULONG conv, len;
  38.     UCHAR buffer[6];
  39.     UCHAR convbuf[32];
  40.     register UCHAR * arg;
  41.  
  42.     /* If no args supplied by user, exit with usage info */
  43.     if ( argc < 2 )
  44.     {
  45.      printf("This program converts ULONGs into the variable length\r\n");
  46.      printf("quantity (ie, series of bytes) equivalent and displays that.\r\n");
  47.      printf("It requires MIDIFILE.DLL to run.\r\n");
  48.      printf("Syntax: MFTOVLQ.EXE [ULONG1 ULONG2...]\r\n");
  49.      exit(1);
  50.     }
  51.  
  52.     /* Convert ascii numerals to byte values, and store in buffer */
  53.     for (i=1; i<argc; i++)
  54.     {
  55.      arg = argv[i];
  56.      if ( *arg == '0' && (*(arg+1) == 'x' || *(arg+1) == 'X') )
  57.      {
  58.           arg+=2;
  59.           conv=0;
  60.           while (*arg)
  61.           {
  62.          conv = (conv << 4) + (ULONG)hex2bin(*(arg)++);
  63.           }
  64.      }
  65.      else
  66.      {
  67.           conv = (ULONG)atoi(arg);
  68.      }
  69.  
  70.      /* Call MIDIFILE.DLL function to do the conversion */
  71.      len = MidiLongToVLQ(conv, &buffer[0]);
  72.  
  73.      /* Format the variable length bytes as ascii and print out */
  74.      printf("%10s = ", argv[i]);
  75.      arg = &buffer[0];
  76.      for (; len; len--)
  77.      {
  78.           printf("0x%02x ", (ULONG)*(arg)++);
  79.      }
  80.      printf("\r\n");
  81.     }
  82.  
  83.     exit(0);
  84. }
  85.