PC Tips and Tricks

Hexadecimal Numbers Explained

The hexadecimal number system is used throughout computing. This article explains what it is.

Why You Want to Know about Hexadecimal

As almost anyone who uses a computer is at least dimly aware, the actual operations of a computer are done with a binary number system. Traditionally, the two possible states for a binary system are represented by the digits for “zero” and “one” although “off” and “on” or sometimes “no” and yes” are closer to what is actually involved. Most of the time a typical PC user has no need to think about this aspect of computers, but every now and then the underlying binary nature will manifest itself. Actually it is a more compact variant of the binary system, called hexadecimal (or “hex” for short) that is likely to be encountered. Since many computer users are totally baffled when they see something written in hex, I am going to point out some of the aspects of this system so that it can be recognized when it is seen. Although interpreting the details of hex code is not of interest to many people, being able to recognize it will at least make computers less mysterious and intimidating. Just being able to say, “Oh yes, that’s hex”, is somehow comforting, even when you have no idea what it means.

What is Hexadecimal?

The reason hex is used in computing is for the convenience of humans since the computer really only understands pure binary. Hex is much more compact since it is based on 16 (the fourth power of 2) instead of 2. Pure binary numbers based on 2 can quickly become unwieldy to write. For example, the three-digit decimal number 513 requires ten digits in pure binary (1000000001) but only three (201) in hex. However, representing hex numbers requires sixteen distinct characters compared to the ten used in decimal notation. The sixteen characters that are used are the ten numeric symbols, 0-9, and the first six letters of the alphabet, A-F. The letters are often, but not always, capitalized. Thus, the first ten numbers 0-9 are written the same in both decimal and hex and the equivalents of the decimal numbers 10-15 are indicated by a single letter, ranging from A to F. Table 1 shows how the decimal numbers 10 -17 are written in hex.

Table 1. Some hex numbers
Decimal 10 11 12 13 14 15 16 17
Hex A B C D E F 10 11

Note that "10" and "11" in hex are not the same as "10" and "11" in decimal. Only the first ten numbers 0-9 are the same in the two representations. To get some feel for what hex numbers are like, let us look at some examples.  Consider the hex number "D8AF". To evaluate the hex number "D8AF", use exactly the same scheme that we use when we write decimal numbers. The various single digits in a decimal number are placeholders representing a multiple of a power of ten. Powers of ten ascend from right to left. For example, the decimal number "365" means 3x102 + 6x10 + 5. In hexadecimal each single character represents a multiple of a power of sixteen. Thus our hex number "D8AF" translated to decimal means 13x163 + 8x162 +10x16 +15 = 55471. Actually going through the arithmetic for hex numbers in order to evaluate them is not really necessary, however. The easier way is to use the calculator that comes as an accessory in Windows. It can convert between decimal and hex when in "Scientific" view.

As can be seen, a hex number such as "365" is not the same as the decimal number "365." The actual value of hex "365" in decimal terms is 3x162 +6x16 +5 = 869. To avoid confusion, hex numbers are often labeled or tagged so that their meaning is clear. One method of tagging hex numbers is to append a lower case "h" at the end. Another method of labeling, used in programming, is to precede the number with 0x. Thus, the hex number "D8AF" could also be written  "D8AFh", where the lower case "h" at the end is just a label to make sure we know it is a hex number. Or it might be written with a labeling prefix as "0xD8AF".

When Does the Average PC User Encounter Hexadecimal Numbers?

Hex notation is found in many places. For example, if you ever do any Windows Regisry editing, you will find certain entries written in hex. Another common occurrence of hex is in JavaScript and other programming lanuages. Also, by convention, addresses of locations in RAM are given in hex. Average PC users may not encounter these examples very often but below are two other uses of hex that I think many will find interesting.

Standard Windows error messages may include hex addresses.

You may have run across a blue screen with a hex code. For example, a common type of error message is the "Fatal exception". The message will contain a statement," A fatal exception <hh> has occurred at <xxxx:xxxxxxxx>", where <hh> stands for some two character hex code that identifies the type of error and the x's are a hex location in memory. An example is "Fatal exception 0E has occurred at 0028:C00082CD." Here "0E" indicates the hex number "zero-E" (not Outlook Express), which means error 14, a page fault. These addresses aren't much help to a typical PC user but may on some occasions be useful to a technician if you are on the phone trying to get help. There is a useful compendium of Windows error messages with various hex addresses at this Microsoft site. The hex addresses may help pinpoint the source of trouble.

Colors on Web Pages

Another place where hex numbers commonly occur is in HTML code where they are used to designate colors. Six characters are used, two for each of the primary colors red, green, and blue. The character "#" is used as an identifying label. Pure red is indicated by #FF0000, pure green by # 00FF00, and pure blue by # 0000FF. By blending various amounts of these primary colors, a large number of shades and colors can be generated. For example, royal blue is indicated by  #4169E1. For those who are interested, take a look at this HTML color picker.