Jump to bottom

Hexadecimal

From Wikipedia, the free encyclopedia

Jump to: navigation, search
Numeral systems by culture
Hindu-Arabic numerals
Eastern Arabic
Indian family
Khmer Mongolian
Thai
Western Arabic
East Asian numerals
Chinese
Counting rods
Japanese Korean
Suzhou
Vietnamese
Alphabetic numerals
Abjad
Armenian
Āryabhaṭa
Cyrillic Ge'ez
Greek (Ionian)
Hebrew
Other systems
Attic
Babylonian
Brahmi
Egyptian
Etruscan
Inuit Mayan
Quipu
Roman
Urnfield
List of numeral system topics
Positional systems by base
Decimal (10)
1, 2, 3, 4, 5, 8, 12, 16, 20, 60 more…

In mathematics and computer science, hexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 09 to represent values zero to nine, and A, B, C, D, E, F (or alternatively a through f) to represent values ten to fifteen. For example, the hexadecimal number 2AF3 is equal, in decimal, to (2 × 163) + (10 × 162) + (15 × 16) + 3, or 10,995.

Each hexadecimal digit represents four binary digits (bits) (also called a "nibble"), and the primary use of hexadecimal notation is as a human-friendly representation of binary coded values in computing and digital electronics. For example, byte values can range from 0 to 255 (decimal) but may be more conveniently represented as two hexadecimal digits in the range 00 through FF. Hexadecimal is also commonly used to represent computer memory addresses.

Representing hexadecimal


0hex = 0dec = 0oct 0 0 0 0
1hex = 1dec = 1oct 0 0 0 1
2hex = 2dec = 2oct 0 0 1 0
3hex = 3dec = 3oct 0 0 1 1

4hex = 4dec = 4oct 0 1 0 0
5hex = 5dec = 5oct 0 1 0 1
6hex = 6dec = 6oct 0 1 1 0
7hex = 7dec = 7oct 0 1 1 1

8hex = 8dec = 10oct 1 0 0 0
9hex = 9dec = 11oct 1 0 0 1
Ahex = 10dec = 12oct 1 0 1 0
Bhex = 11dec = 13oct 1 0 1 1

Chex = 12dec = 14oct 1 1 0 0
Dhex = 13dec = 15oct 1 1 0 1
Ehex = 14dec = 16oct 1 1 1 0
Fhex = 15dec = 17oct 1 1 1 1

In situations where there is no context, a hexadecimal number might be ambiguous and confused with numbers expressed in other bases. There are several conventions for expressing values unambiguously. A numerical subscript (itself written in decimal) can give the base explicitly: 15910 is decimal 159; 15916 is hexadecimal 159, which is equal to 34510. Other authors prefer a text subscript, such as 159decimal and 159hex, or 159d and 159h.

In linear text systems, such as those used in most computer programming environments, a variety of methods have arisen:

  • In URLs, character codes are written as hexadecimal pairs prefixed with %: http://www.example.com/name%20with%20spaces where %20 is the space (blank) character (code value 20 in hex, 32 in decimal).
  • In XML and XHTML, characters can be expressed as hexadecimal numeric character references using the notation &#xcode;, where code is the 1- to 6-digit hex number assigned to the character in the Unicode standard. Thus ’ represents the curled right single quote (Unicode value 2019 in hex, 8217 in decimal).
  • Color references in HTML and CSS can be expressed with six hexdigits (two each for the red, green, and blue components, in that order) prefixed with #: white, for example, is represented #FFFFFF .[1] CSS allows 3-hexdigit abbreviations with one hexdigit per component: #FA3 abbreviates #FFAA33 (a golden orange).
  • *nix (UNIX and related) shells, and likewise the C programming language, which was designed for UNIX (and the syntactical descendants of C[2]) use the prefix 0x for numeric constants represented in hex: 0x5A3. Character and string constants may express character codes in hexadecimal with the prefix \x followed by two hex digits: '\x1B' represents the Esc control character; "\x1B[0m\x1B[25;1H" is a string containing 11 characters (plus a trailing NUL to mark the end of the string) with two embedded Esc characters.[3] To output an integer as hexadecimal with the printf function family, the format conversion code %X or %x is used.
  • In the Unicode standard, a character value is represented with U+ followed by the hex value: U+20AC is the Euro sign (€).
  • In MIME (e-mail extensions) quoted-printable encoding, characters that cannot be represented as literal ASCII characters are represented by their codes as two hexadecimal digits (in ASCII) prefixed by an equal to sign =, as in Espa=F1a to send "España" (Spain). (Hexadecimal F1, equal to decimal 241, is the code number for the lower case n with tilda in the ISO/IEC 8859-1 character set.)
  • In Intel-derived assembly languages, hexadecimal is indicated with a suffixed H or h: FFh or 05A3H. Some implementations require a leading zero when the first hexadecimal digit character is not a decimal digit: 0FFh
  • Other assembly languages (6502, AT&T, Motorola), Pascal, Delphi, some versions of BASIC (Commodore) and Forth use $ as a prefix: $5A3.
  • Some assembly languages (Microchip) use the notation H'ABCD' (for ABCD16).
  • Ada and VHDL enclose hexadecimal numerals in based "numeric quotes": 16#5A3#. VHDL also supports x"5A3".
  • Verilog represents hexadecimal constants in the form 8'hFF, where 8 is the number of bits in the value and FF is the hexadecimal constant.
  • Modula 2 and some other languages use # as a prefix: #05A3
  • The Smalltalk programming language uses the prefix 16r: 16r5A3
  • Postscript indicates hex with prefix 16#: 16#5A3. Binary data (such as image pixels) can be expressed as unprefixed consecutive hexadecimal pairs: AA213FD51B3801043FBC...
  • Common Lisp use the prefixes #x and #16r.
  • QuickBASIC, FreeBASIC and Visual Basic prefix hexadecimal numbers with &H: &H5A3
  • BBC BASIC and Locomotive BASIC use & for hex.[4]
  • TI-89 and 92 series uses a 0h prefix: 0h5A3
  • Notations such as X'5A3' are sometimes seen, such as in PL/I. This is the most common format for hexadecimal on IBM mainframes (zSeries) and midrange computers (iSeries) running traditional OS's (zOS, zVSE, zVM, TPF, OS/400), and is used in Assembler, PL/1, Cobol, JCL, scripts, commands and other places. This format was common on other (and now obsolete) IBM systems as well.
  • Donald Knuth introduced the use of a particular typeface to represent a particular radix in his book The TeXbook.[5] Hexadecimal representations are written there in a typewriter typeface: 5A3
Bruce A. Martin's hexadecimal notation proposal

There is no universal convention to use lowercase or uppercase for the letter digits, and each is prevalent or preferred in particular environments by community standards or convention.