Hexadecimal: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
(Extended, including conversions from bin to hex without decimal.)
Line 1: Line 1:
[[Category:Encyclopedia]]Hexadecimal code is another number system used by computers. While decimal (our normal number system) uses ten digits, and binary uses two digits (0 and 1), hexadecimal uses base 16.
[[Category:Encyclopedia]]Hexadecimal is a counting system that uses sixteen digits.


Instead of using only 0's and 1's like binary, or 0-9 like decimal, hexadecimal uses 0-9 just like base 10, but also A-F. A=10, B=11, and so forth. The most convenient method of converting binary to hexadecimal (for a human) is to first separate the binary number into string of 4 digits. Then, convert each group into base 10, at which point 10-15 are changed into letters.
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 ==
Ex.
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.
10010111

1001 0110
== Comparing counts from zero in different number systems ==
2^3+2^0 2^2+2^1+2^0
8+1 4+2+1
Binary
9 7
Decimal
97
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 ==
# Split a binary number into groups of four digits, counting from right to left.
# Pad the leftmost group of binary digits with zeros on their left if their are less than four digits.
# 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

Revision as of 04:43, 31 January 2009

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.

Comparing counts from zero in different number systems

     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