home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR2 / CACODE.ZIP / BYTESOUT.C < prev    next >
C/C++ Source or Header  |  1993-07-13  |  1KB  |  67 lines

  1. #include <extend.h>
  2.  
  3. int __b2int(char *, int);
  4. char *__strsub(char *, char *, int, int);
  5.  
  6.  
  7. CLIPPER Make_cByte(void) {
  8.  
  9.     char *cBits = _parc(1);
  10.     int len = _parclen(1);
  11.  
  12.     char BitTemp[7] = "";
  13.     char cBytesOut[5] = "";
  14.  
  15.  
  16.  
  17.     cBytesOut[0] = (char)( 48+(__b2int(__strsub(cBits,BitTemp, 0,6),5)) );
  18.     cBytesOut[1] = (char)( 48+(__b2int(__strsub(cBits,BitTemp, 6,6),5)) );
  19.     cBytesOut[2] = (char)( 48+(__b2int(__strsub(cBits,BitTemp,12,6),5)) );
  20.     cBytesOut[3] = (char)( 48+(__b2int(__strsub(cBits,BitTemp,18,6),5)) );
  21.     cBytesOut[4] = '\0';
  22.  
  23.     _retc(cBytesOut);
  24. }
  25.  
  26.  
  27. char *__strsub(char *bits, char *retval, int from, int digits ) {
  28.  
  29.     int i;
  30.     int j;
  31.  
  32.     for (i=from,j=0;j<digits;i++,j++) {
  33.  
  34.         retval[j] = bits[i];
  35.  
  36.     }
  37.  
  38.     retval[6] = '\0';
  39.  
  40.     return(retval);
  41.  
  42. }
  43.  
  44.  
  45. int __b2int(char *num, int len) {
  46.  
  47.     // len could be calculated, but it is faster if you just tell it how
  48.     // long the string is. REMEMBER the string length is 1 less than the
  49.     // actual length.  C begins array indexing at 0, so a six character
  50.     // string is actually positions 0-5!   Also, notice that the len var
  51.     // is checked until it reaches -1.  We need the "0" position as it is
  52.     // the first character in the string.
  53.  
  54.     int i = 0;
  55.     int j = 0;
  56.  
  57.     for (j=0 ; len != -1 ; j++) {
  58.  
  59.         if (num[len--] == '1')
  60.             i += __exponent(2,j);
  61.  
  62.     }
  63.  
  64.     return(i);
  65. }
  66.  
  67.