Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Wednesday, September 29, 2010

Hexadecimal Number System


Continuation of earlier Articles:

  1. Learn Binary Number System
  2. Learn Binary Number System-2
  3. Octal Number System

Hexadecimal Numbers are yet another method to write Binary Values into shorter forms than Octal Numbers.  This Number System has the Number 16 as its Base.  As per the basic rules, which we are following to devise new Number Systems, the Base-16 Number System has its Numeral values from 0 to 15 (i.e., one less than the base value 16).  Like the decimal number system, the hexadecimal number system also has digit values from 0 to 9. Since there are no single digit values for 10 to 15 the alphabets A to F are used.

Hexadecimal Decimal
 0            0
 1            1
 2            2
 3            3
 4            4
 5            5
 6            6
 7            7
 8            8
 9            9
 A           10
 B           11
 C           12
 D           13
 E           14
 F           15

Now, let us see how we can convert binary numbers into hexadecimal form. We will use the same method we have used for converting Binary to Octal form.  We have made groups of 3 binary digits of binary value 011,111,111 (equal to 255 decimal) to find the Octal Number &O377.  For Hexadecimal Number, we must take groups 4 binary digits (Binary 1111 = (1+2+4+8) = 15, the maximum value of a hexadecimal digit) and add the values of binary digits to find the hexadecimal digit.

Let us find the Hexadecimal value of Decimal Number 255.

Decimal 255 = Binary 1111,1111 = Hexadecimal digits FF

It takes 3 digits to write the quantity 255 in decimal as well as in Octal 377 (this may not be true when bigger decimal numbers are converted into Octal) but in Hexadecimal it takes only two digits: FF or ff (not case sensitive).

Let us try another example:

Decimal Number 500 = Binary Number 111110100

     111,110,100 =  Octal Number 764

0001,1111,0100 = Hexadecimal Number 1F4

To identify Octal Numbers we have used prefix characters &O or &0 with the Number.  Similarly, Hexadecimal numbers have &H (not case sensitive) as the prefix to identify when entered into computers like &H1F4, &hFF, etc.

You may type Print &H1F4 in Debug Window and press Enter Key to covert and print its Decimal Value.

MS-Access Functions

There are only two conversion Functions: Hex() and Oct() in Microsoft Access, both use Decimal Numbers as the parameter.

Try the following Examples, by typing them in the Debug Window to convert a few Decimal Numbers to Octal and Hexadecimal:

? OCT(255)
Result: 377

? &O377
Result: 255

? HEX(255)
Result: FF

? &hFF
Result: 255

? HEX(512)
Result: 200

? &H200
Result: 512

MS-Excel Functions

In Microsoft Excel, there are Functions for converting values to any of these forms.  The list of functions is given below:

Simple Usage: X = Application.WorksheetFunction.DEC2BIN(255)

Decimal Value Range -512 to +511 for Binary.

  1. DEC2BIN()
  2. DEC2OCT()
  3. DEC2HEX()
  4. BIN2DEC()
  5. BIN2OCT()
  6. BIN2HEX()
  7. OCT2DEC()
  8. OCT2BIN()
  9. OCT2HEX()
  10. HEX2DEC()
  11. HEX2OCT()
  12. HEX2BIN()

You can convert Decimal Value to a maximum of 99,999,999 to Octal Number with the Function DEC2OCT() and its equal value of Octal to Decimal OCT2DEC() Function.

499,999,999,999 is the maximum decimal value for DEC2HEX() and its equal value in Hexadecimal form for HEX2DEC() Function.

With the Binary functions you can work with the Decimal value range from -512 to 511 up to a maximum of 10 binary digits (1111111111 the left most bit is the sign bit, i.e. if it is 1 then the value is negative, and when 0 is positive).

We have created two Excel Functions in Access to convert Decimal Numbers to Binary and Binary Values to Decimal.

But first, you have to attach the Excel Application Object Library file to the Access Object References List.

  1. Open VBA Window, select References from the Tools Menu and look for Microsoft Excel xx.0 Object Library File, and put a checkmark to select it.
  2. Copy the following VBA Code and Paste it into a Standard Module:

Public Function DEC2_BIN(ByVal DEC As Variant) As Variant
Dim app As Excel.Application
Dim obj As Object

Set app = CreateObject("Excel.Application")
Set obj = app.WorksheetFunction
DEC2_BIN = obj.DEC2BIN(DEC)

End Function

Public Function BIN2_DEC(ByVal BIN As Variant) As Variant
Dim app As Excel.Application
Dim obj As Object

Set app = CreateObject("Excel.Application")
Set obj = app.WorksheetFunction
BIN2_DEC = obj.Bin2Dec(BIN)

End Function

Demo Run from the Debug Window and the output values are shown below:

? DEC2_BIN(-512)
1000000000

? DEC2_BIN(-500)
1000001100


? DEC2_BIN(511)
111111111

? BIN2_DEC(1000000000)
-512 

? BIN2_DEC(111111111)
 511 
In the first example, if the leftmost Bit (10th-bit value is -512) is 1 then the value is negative. For positive values, 9 bits are used for up to a maximum decimal value of 511.  This limitation is only for the Functions and as you are aware computers can handle very large positive/negative numbers.

In the first example, you can see that the leftmost sign Bit is 1, in the binary value 512 position, indicating that it is a negative value, and the other positive value Bits are all zeroes. 

In the second example, the number -500 output in binary shows that the positive value Binary Bit value at 4 + 8 is On. That means the positive value 12 is added to -512 resulting in the value -500.

You can convert any value between -512 to 511 for Binary value conversions, as we have stated earlier with the above functions.  

You may implement other functions given in the Excel Function list, in a similar way in Access, if needed.

Technorati Tags:

Earlier Post Link References:

  1. Learn Binary Numbering System
  2. Learn Binary Numbering System-2
  3. Octal Numbering System
  4. Hexadecimal Numbering System
  5. Colors 24-Bits And Binary Conversion.
  6. Create Your Own Color Palette

No comments:

Post a Comment

Comments subject to moderation before publishing.

Powered by Blogger.