Hexadecimal

From StealthBot Wiki Backup
Revision as of 08:20, 1 December 2017 by Davnit (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

In computer science, hexadecimal is a base-16 numbering system (as opposed to the standard base-10 system called decimal). In representation, it uses sixteen distinct symbols: the symbols 0 to 9 to represent values zero to nine, and A, B, C, D, E, F (or alternatively lowercase) to represent the 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), and the primary use of hexadecimal notation is as a human-friendly representation of binary coded values in computing. 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.

Binary

In order to understand hexadecimal, you need to know that every number stored in computer memory is stored in "binary" format: 1s and 0s. Binary is the base-2 number system (as compared to the common base-10 number system, decimal) that allows us to describe the value of a number in the memory of a computer more accurately. A "bit" is each 1 or 0 digit in a binary number. A "byte" is 8 of these bits. A VBScript integer takes up 2 bytes (16-bit number) and a long takes up 4 bytes (32-bit number).

A binary number may be represented in text with a suffix of b. Here we will be representing in in a format similar to:

0000 0000b

Where bit 1 (1s place or "least-significant" bit) is on the right before the b, 2s place next to it, 4s place next, and so on (in a similar manner that you would say 1s place, 10s place, 100s place for decimal).

VBScript has no way of taking in a binary number as a literal. Instead, you may use hexadecimal.

Hexadecimal representation

Hexadecimal numbers are often represented in text with a prefix of 0x or sometimes a suffix of h, so they are not confused with decimal numbers.

Using hexadecimal to represent binary values is often more useful than using decimal because two hexadecimal digits always translates to one byte:

binary:           0101 1010b
value by nibble: |  5 | 10 |
hexadecimal:    0x5A
decimal:          90

A "nibble" is just another word for a four-bit hexadecimal digit.

Hexadecimal in VBScript

To represent a hexadecimal number in VBScript code, you must prefix the number with &H. For example, the number 0x5A would appear &H5A.

To generate a string containing a hexadecimal number, use the Hex() function, since trying to display a number represented with the &H prefix will only result in the decimal value.

VBScript colors

Colors in VBScript are represented in the red-green-blue (or RGB) model with three values: the intensity of red, green, and blue respectively. Each intensity value ranges from 0 (0% or 0x00) to 255 (100% or 0xFF). Colors are stored in a 32-bit number (VBScript Long) where the first four least significant bits represent red, the next four green, and the most significant four blue.

For example, here are some common colors represented in hexadecimal (VBScript code):

'          BBGGRR - for visualization, BB is blue, GG is green, and RR is red
Black  = &H000000 ' decimal: 0
Red    = &H0000FF ' 255
Green  = &H00FF00 ' 65280
Blue   = &HFF0000 ' 16711680
White  = &HFFFFFF ' 16777215
Yellow = &H00FFFF ' 65535
Orange = &H00A5FF ' 42495

Note that since hexadecimal representation is not made specifically for representing colors, it does not require zeros on the left (between the &H and the number), but will ignore them. They should be used to make the hexadecimal number 6 digits as a visual cue that it is meant to be used as a color.

To generate colors, many common methods are used:

  • Use a known value in RGB format and pass the three parts to the built-in RGB() function.
  • Use a predefined color from StealthBot's Color object.
  • Use a predefined color constant.
  • Use a known CSS color value (such as #ffcc22) and reverse it (since CSS color values are in the hexadecimal format #RRGGBB which is opposite to the format needed for VB) to make a hex value (&H22CCFF).

See also