home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / prof_c / util / hex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  919 b   |  53 lines

  1. /*
  2.  *    hex.c -- hex conversions routines
  3.  */
  4.  
  5. #define NIBBLE    0x000F
  6.  
  7. char hextab[] = {
  8.     '0', '1', '2', '3', '4', '5', '6', '7',
  9.     '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  10. };
  11.  
  12. /*
  13.  *    byte2hex -- convert a byte to a string
  14.  *    representation of its hexadecimal value
  15.  */
  16.  
  17. char *
  18. byte2hex(data, buf)
  19. unsigned char data;
  20. char *buf;
  21. {
  22.     register char *cp;
  23.  
  24.     cp = buf;
  25.     *cp++ = hextab[(data >> 4) & NIBBLE];
  26.     *cp++ = hextab[data & NIBBLE];
  27.     *cp = '\0';
  28.  
  29.     return (buf);
  30. } /* end byte2hex() */
  31.  
  32. /*
  33.  *    word2hex -- convert a word to a string
  34.  *    representation of its hexadecimal value
  35.  */
  36.  
  37. char *
  38. word2hex(data, buf)
  39. unsigned int data;
  40. char *buf;
  41. {
  42.     register char *cp;
  43.  
  44.     cp = buf;
  45.     *cp++ = hextab[(data >> 12) & NIBBLE];
  46.     *cp++ = hextab[(data >> 8) & NIBBLE];
  47.     *cp++ = hextab[(data >> 4) & NIBBLE];
  48.     *cp++ = hextab[data & NIBBLE];
  49.     *cp = '\0';
  50.  
  51.     return (buf);
  52. } /* end word2hex() */
  53.