Hexadecimal

From Rosetta Code

Hexadecimal is a counting system that uses sixteen digits.

Instead of using only 0's and 1's like binary, or the characters 0 to 9 of the decimal number system; hexadecimal uses the characters '0' to '9' to represent the numbers 0 to 9, and also the single characters 'A' to 'F' (or sometimes 'a' to 'f', but usually not mixing case), to represent the numbers 10 through to 15, in order.

Uses

The hexadecimal number system is used widely in the Electronics and Computer Industry, as although digital electronics is based on gates with only two states and is therefore fundamentally binary, binary numbers can quickly become long and hard to transcribe without errors. Their hexadecimal equivalents are much shorter and easier to remember, and have a straight-forward way of conversion to/from binary.

Hex Dump

A textual representation of data where values are expressed in hexadecimal. Often used to show the contents of regions of memory where both the memory addresses as well as the memory contents may be expressed in hexadecimal.

Comparing counts from zero in different number systems

C.f. Common number base conversions

     Binary
           Decimal
                 Hexadecimal
     0     0     0
     1     1     1
    10     2     2
    11     3     3
   100     4     4
   101     5     5
   110     6     6
   111     7     7
  1000     8     8
  1001     9     9
  1010    10     A
  1011    11     B
  1100    12     C
  1101    13     D
  1110    14     E
  1111    15     F
 10000    16    10
 10001    17    11
 10010    18    12
 10011    19    13
 10100    20    14
 10101    21    15
 10110    22    16
 10111    23    17
 11000    24    18
 11001    25    19
 11010    26    1A
 11011    27    1B
 11100    28    1C
 11101    29    1D
 11110    30    1E
 11111    31    1F
100000    32    20
100001    33    21

Converting binary to hexadecimal

  1. Split a binary number into groups of four digits, counting from right to left.
  2. Pad the leftmost group of binary digits with zeros on their left if their are less than four digits.
  3. Use the following table to translate each group of four binary digits, in order, to its hexadecimal equivalent.
Binary digits
      Hexadecimal equivalent digit
0000  0
0001  1
0010  2
0011  3
0100  4
0101  5
0110  6
0111  7
1000  8
1001  9
1010  A
1011  B
1100  C
1101  D
1110  E
1111  F

An example conversion

     Binary Number:     1011010111
             Split:   10 1101 0111
               Pad: 0010 1101 0111
  Translate groups:    2    D    7
Hexadecimal answer: 2D7

Hex words

It is common when working at the assembler level, and when designing digital hardware to arrange for self-checking tests to denote pass or failure by writing distinctive values to a memory location, or for un-initialised memory to be distinctive values. Many engineers use values whose value when expressed in a hex dump stands out as valid english words such as the values DEAD and BEEF.

A scan of the word list used in Anagrams leads to this list of four digit hexadecimal values that are also words:

ABBE ABED BABE BADE BEAD BEEF CAFE CEDE DADA DADE DEAD DEAF DEED FACE FADE FEED