#1,080 – Binary Numerals Written as Hexadecimal

Binary data, for example data stored in in some location in memory, is typically written as a hexadecimal number.  This can be done by grouping the binary digits into groups of four.  Each group of four digits can then be represented by a single hexadecimal character.

Four binary digits can range from 0000 to 1111, representing values from 0 to 15, corresponding to the 16 available characters in the hexadecimal number system.

1080-001

The example below shows how we can represent a long binary number (16 bits in this case) as a series of hex characters.

1080-002

The practice of grouping binary data into groups of four digits maps well to data stored in digital computers, since the typical size of a data word in a binary computer is some factor of four–e.g. 16 bits, 32 bits, or 64 bits.  These groups of bits can then be represented by 4, 8, or 16 hexadecimal characters, respectively.

Advertisements

#1,078 – The Hexadecimal Numeral System

Humans normally represent numeric values using the decimal (base 10) numeral system.  We can also represent numbers as hexadecimal, or base 16.

The hexadecimal numeral system uses 16 different digits (numeric symbols), rather than 10.  You represent a numerical value using a string of these hexadecimal (or hex) digits.  The 16 digits used are the digits 0-9 and the letters A-F.  (E.g. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F).  Comparing to the decimal system, A then represents 10, B represents 11, up to F, which represents 15.  Hexadecimal constants are often written as “0x”, followed by the digits.

Moving from right to left, the digits in a base 16 system represent powers of 16 (1, 16, 256, 4096, etc. or 16^0, 16^1, 16^2, 16^3, etc).  The value of a number represented as hexadecimal is the sum of the value of each digit multiplied by the appropriate power of 16.  For example:

1078-001

As an example, the hexadecimal number 0x3A8E is equivalent to the decimal number 14,990.

1078-002

 

 

 

 

#989 – Formatting Numbers as Hexadecimal

You can represent an integer as a series of hexadecimal characters by using the hexadecimal format specifier in a format string.

In the examples below, we represent several integers using their hexadecimal characters.  We use the X notation in the format string to indicate that the number should be displayed as hex.

            Console.WriteLine("{0} = {0:X}", 10);    // A
            Console.WriteLine("{0} = {0:X}", 157);   // 9D = (9 * 16) + 13
            Console.WriteLine("{0} = {0:X}", 1024);  // 400 = (4 * 256)
            Console.WriteLine("{0} = {0:X}", 6975);  // 1B3F = (1 * 4096) + (11 * 256) + (3 * 16) + 15

989-001
You can include a digit after the X to indicate the number of minimum digits to be displayed. The hex number will be padded with zeroes to reach the desired width.

            Console.WriteLine("{0} = {0:X4}", 157);   // 009D

989-002

#72 – Hexadecimal Numbers

In C#, integer literals are normally specified using base-10 notation (e.g. 123), but can also be specified as a base-16 (hexadecimal or hex) number.

Each hex digit represents a 4-bit value and can therefore represent a value in the range [0,15].  Values from 0-9 are represented by their decimal digits.  Values from 10-15 are represented by the hex digits A-F.

In C#, hex literals begin with the characters “0x”.

Each hex digit represents a value to be multiplied by a power of 16.

Example: 0x1A2F = (1 x 163) + (10 x 162) + (2 x 161) + (15 x 160) = 4096 + 2560 + 32 + 15 = 6703

You can also think of each hex digit as representing four bits:

0 = 0000
1 = 0001
2 = 0010

E = 1110
F = 1111

So 0x1A2F would be:  0001 1010 0010 1111

In C#, you can use hex numbers for integer literals.

 int n = 0x1A2F;
 ushort u1 = 0xFFFF;         // 16 bits
 uint u2 = 0x12341234;       // 32 bits

Hex numbers are a convenient way of expressing integral values, denoting exactly the bits stored in memory for that integer.