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

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