Hexadecimal
From C64-Wiki
The hexadecimal number system is a denominational number system on the base of 16.
Contents |
[edit] General
The hexadecimal number system is a denominational number system on the base of 16. The ten digits of the decimal system (0 to 9) therefor are not enough and are supplemented through the letters A to F for the decimal values 10 to 15. Ever four digits of the Dual Systems can now be allegorized with only one digit of the hexadecimal system and it's through this a very easy to learn abbreviated form for the binary numbers (half byte or Nibble, see also Byte). The hexadecimal diction is often used in machine near programming languages e.g. Assembler, to assign memory addresses or memory contents.
For better differentiation between hexadecimal and decimal numbers is often used a prefix or ending like "h", "&h" or "0x". By the C64 is the Dollar symbol ("$") used as an prefix. Under mathematic aspects the hexadecimal number receives an annex in form of a lowered (16). For example: dec. 123 = bin. 01111011 = $7B = &h7B = 7Bh = 0x7B = 7B16.
The advantage of the hexadecimal system in combination with computers is that from the view of the computer the used binary coding "even" numbers like e.g. 1000000002 in decimal system look very "awry" (256(10) in this case), as the same number in hexadecimal form relativly readable stays (100(16) in this case) what if you look in the memory plan of the C64 annoy immediately.
[edit] Conversion table
| Hexadecimal no. | Binary no.
(as Nibble) | Decimal no. |
|---|---|---|
| 0 | 0000 | 0 |
| 1 | 0001 | 1 |
| 2 | 0010 | 2 |
| 3 | 0011 | 3 |
| 4 | 0100 | 4 |
| 5 | 0101 | 5 |
| 6 | 0110 | 6 |
| 7 | 0111 | 7 |
| 8 | 1000 | 8 |
| 9 | 1001 | 9 |
| A | 1010 | 10 |
| B | 1011 | 11 |
| C | 1100 | 12 |
| D | 1101 | 13 |
| E | 1110 | 14 |
| F | 1111 | 15 |
[edit] Conversion
For programmers are interesting conversion methods, e.g. how could be a hexadecimal number be converted in a decimal number or which decimal number results from the existing hexadecimal number.
[edit] Hexadecimal- to Decimal number
For example we will use the binary number 9F.
In advance the F must be implemented in the formula as 15:
PRINT "DECIMAL NO.: "9*16↑1+15*16↑0"
And the monitor output says: DECIMAL NO: 159
[edit] Decimal- to Hexadecimal number
The conversion from decimal to hexadecimal is a little bit more difficult. A simple method is the division method. By this method the decimal number will be so long divided through the base 16 until the result is 0, at which the rest will be noticed which reverse read in the hexadecimal number results. Additional it must be considered that the rest 10 to 15 must be conversed in the hex digits A to F.
159 : 16 = 9 Rest ..15 009 : 16 = 0 Rest .9 ----------------------- Result Hexadecimal: 9F
As an BASIC programm:
10 PRINT "=== DECIMAL TO HEXADECIMAL ===" 20 INPUT "DECIMAL NO.:";A$ 30 A = ABS( VAL(A$) ) 40 B = INT(A/16) 41 A = A - (B*16) 50 IF A=10 THEN C$ =C$ +"A" 51 IF A=11 THEN C$ =C$ +"B" 52 IF A=12 THEN C$ =C$ +"C" 53 IF A=13 THEN C$ =C$ +"D" 54 IF A=14 THEN C$ =C$ +"E" 55 IF A=15 THEN C$ =C$ +"F" 56 IF A<10 THEN C$ =C$ +MID$( STR$(A),2,1 ) 57 A = B : IF B<>0 THEN 40 60 FOR X = LEN(C$) TO 1 STEP -1 61 B$ = B$ + MID$(C$,X,1) 62 NEXT X : PRINT "HEXADECIMAL:" B$
Comment: The programm works in 2 parts. In the rows 40 to 57 the division is made and in rows 60 to 61 the reversion (from the wrong cached result F9 will be made the right result 9F).
[edit] Binary no. to Hexadecimal
Long binary numbers are simply splitted from right to left in 4 Bit Units. From these you can easily convert with the above table the hexadecimal numbers:
1011.0010.1100.1001 entspricht B2C9
[edit] Hexadecimal to binary no.
Because every hexadecimal number position consists of 4 Bits, you can fast convert with above table a hexadecimal no. in a binary no.:
F3A6 equates to 1111.0011.1010.0110
[edit] Links
| Wikipedia: Hexadecimal |
