# okay

by (Login MCalkins)
Moderator

Here is a decimal version:

'public domain
DIM k AS STRING
DO
DO
k = INKEY\$
LOOP UNTIL LEN(k)
PRINT CHR\$(34); k; CHR\$(34), ; 'some control chars will have effect
SELECT CASE LEN(k)
CASE 1
PRINT "chr\$("; LCASE\$(LTRIM\$(STR\$(ASC(k)))); ")";
CASE 2
PRINT "chr\$(0) + chr\$("; LCASE\$(LTRIM\$(STR\$(ASC(RIGHT\$(k, 1))))); ")";
END SELECT
PRINT SPACE\$(40)
LOOP UNTIL k = CHR\$(27)
END

Note that the some key combinations produce control characters, that when PRINTed, perform certain actions. Ctrl+g causes a beep, ctrl+i causes a tab, ctrl+j causes a carriage return, crtl+k seems to move the cursor to the top of the page (thus obscuring the output from future keystrokes), ctrl+l clears the screen, and ctrl+m is a line feed.

For your information:

Decimal is a base 10 number system. Each place value is a power of 10, and there are 10 numerals, "0" to "9".

For example:
decimal 27589
= 2*10^4 + 7*10^3 + 5*10^2 + 8*10^1 + 9*10^0

= 2*10000 + 7*1000 + 5*100 + 8*10 + 9*1

= 20000 + 7000 + 500 + 80 + 9

= 25789

Binary is a base 2 number system. Each place value is a power of 2, and there are 2 numerals, "0" to "1". As you probably know, computers operate with binary. Each binary place is called a bit.

For example:
binary 0101 1101
= 0*2^7 + 1*2^6 + 0*2^5 + 1*2^4 + 1*2^3 + 1*2^2 + 0*2^1 + 1*2^0

= 0*128 + 1*64 + 0*32 + 1*16 + 1*8 + 1*4 + 0*2 + 1*1

= 0 + 64 + 0 + 16 + 8 + 4 + 0 + 1

= 93

Hexadecimal, also known as "hex", is a base 16 number system. Each place value is a power of 16, and there are 16 numerals, "0" to "9", and "a" to "f". ("a"=ten,, ..., "f"=fifteen).

For example:
hexadecimal 7f3c
= 7*16^3 + 15*16^2 + 3*16^1 + 12*16^0

= 7*4096 + 15*256 + 3*16 + 12*1

= 28672 + 3840 + 48 + 12

= 32572

The wonderful thing about hexadecimal is that since 16 is a power of 2, there is a direct correspondence between binary places and hexadecimal places. Specifically, 16 is 2^4, so every hexadecimal place corresponds to exactly 4 binary places:

bin .. hex
0000 = 0
0001 = 1
0010 = 2
0011 = 3
0100 = 4
0101 = 5
0110 = 6
0111 = 7
1000 = 8
1001 = 9
1010 = a
1011 = b
1100 = c
1101 = d
1110 = e
1111 = f

That correspondence applies to every correctly aligned 4 bit group. (By correctly aligned, I mean that you start from the right hand side, the lowest place value, and break the binary number up into 4 bit groups, from right to left.) There is no such correspondence with decimal, because 10 is not a power of 2.

So, you can use that list to quickly convert from hex to bin or from bin to hex. Just using that list, you can convert binary 0101 1101 to hex 5d, and you can convert hex 7f3c to 0111 1111 0011 1100 binary.

Computers use binary, but binary is long and clumsy from the perspective of human readers. Hexadecimal can be thought of as compressed binary, since it compresses every 4 binary places to 1 hexadecimal place. The hex numerals reveal the bit pattern. So, hexadecimal has the advantage of compactness, and the advantage of revealing the bit pattern that matters to the computer.

The importance of binary numbers and bit patterns will become more evident when you start using the bitwise logic operators, such as AND, OR, XOR, and NOT. These operators work on individual bits.

Several appendix sections in my unfinished book are relevant here.

Right click the link, select "Save Link As..." or "Save Target As...", and save it as a Text Document. Then, open it in Notepad.

http://www.qbasicmichael.com/excerpt.txt

Regards,
Michael

 This message has been edited by MCalkins on Oct 1, 2011 5:40 AMThis message has been edited by MCalkins on Oct 1, 2011 12:43 AM

Posted on Oct 1, 2011, 12:37 AM