Base Converson in any Base
Here we use the greedy algorithm to convert 431 to base 5.
The "denominations" are powers of 5. Use as many as you can at each level.
431 1 1 0 1 all used up 5 1 1 1 5 so 1 left 25 2 6 2 25s so 6 left 125 3 56 3 125s, so 56 left 625 0 431 no 625s
Read up the middle column to see the base-5 expansion.
431 = 32115
Here is the littleendian method. IT works the same way for any base.
431 *1 431/5 = 86 r 1 86 *11 86/5 = 17 r 1 17 *211 17/5 = 3 r 2 3 *3211 3/5 = 0 r 2 0 3211 DONE
Octal and Hexadecimal Numbers
Base 8 is octal and base 16 is hexadecimal.
A base 8 digit is 0-7. Each base 8 digit is three bytes of information. Converting from binary is easy. Just use the packing algorithm.
Start at the little end and divide the bits into groups of 3. Then convert each group into an octal digit 0-7.
1010111101010101 start 1 010 111 101 010 101 break into trios 1 2 7 5 2 5 → 0o127525 translate each trio.
Base 16 is hexadecimal. Its alphabeet is
Σ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E F}.
When converting from binary to hex, break into groups of four.
1010111101010101 start 1010 1111 0101 0101 break into quartets A F 5 5 → 0xAF55 assemble. 0x means "hex number"
Both procedures are easily reversed. Each octal digit expands into three bits, each hex, four. This is "unpacking." Here are examples
0x24A9 (unpacking) 0010 0100 1010 1001 unpack into foursies. 0b0010010010101001 glue 'em.
0o423 100 010 011 unpack into threesies. 0o100010011 glue 'em
Everything is Bits!
Consider this file.
abcdefghijklnnopqrstuvwxyz ABCDEFGHIJKLNNOPQRSTUVWXYZ 0123456789!@#$%^&*()_-+=<>
The characters you see are an illusion. I shall take you down the rabbit hole. First, look at this hex dump of the file.
0000000: 6162 6364 6566 6768 696a 6b6c 6e6e 6f70 abcdefghijklnnop 0000010: 7172 7374 7576 7778 797a 0a41 4243 4445 qrstuvwxyz.ABCDE 0000020: 4647 4849 4a4b 4c4e 4e4f 5051 5253 5455 FGHIJKLNNOPQRSTU 0000030: 5657 5859 5a0a 3031 3233 3435 3637 3839 VWXYZ.0123456789 0000040: 2140 2324 255e 262a 2829 5f2d 2b3d 3c3e !@#$%^&*()_-+=<> 0000050: 0a
Here is how toread it. The first column is the number of the
first byte. It is a hex number. Look at the first line;
the first two digits you see are 61; this is hex for 97.
The far right column starts with an a
. When your computer
is storing a
it is actually storing the byte 0x61.
Observe that b
is 0x62. All of the lower-case letters are
stored consecutively.
Notice that A
has byte value 0x41, or 65 in decimal.
All of the upper-case letters have consecutive bytr values. The digit
0
has byte value 0x30 = 48.
So, characters are an illusion! Your computer renders them visually, but under the hood they are just bytes.
Now we do a binary dump of the file.
0000000: 01100001 01100010 01100011 01100100 01100101 01100110 abcdef 0000006: 01100111 01101000 01101001 01101010 01101011 01101100 ghijkl 000000c: 01101110 01101110 01101111 01110000 01110001 01110010 nnopqr 0000012: 01110011 01110100 01110101 01110110 01110111 01111000 stuvwx 0000018: 01111001 01111010 00001010 01000001 01000010 01000011 yz.ABC 000001e: 01000100 01000101 01000110 01000111 01001000 01001001 DEFGHI 0000024: 01001010 01001011 01001100 01001110 01001110 01001111 JKLNNO 000002a: 01010000 01010001 01010010 01010011 01010100 01010101 PQRSTU 0000030: 01010110 01010111 01011000 01011001 01011010 00001010 VWXYZ. 0000036: 00110000 00110001 00110010 00110011 00110100 00110101 012345 000003c: 00110110 00110111 00111000 00111001 00100001 01000000 6789!@ 0000042: 00100011 00100100 00100101 01011110 00100110 00101010 #$%^&* 0000048: 00101000 00101001 01011111 00101101 00101011 00111101 ()_-+= 000004e: 00111100 00111110 00001010 <>.
Here we see the bytes in their full binary glory! Observe taht a newline has byte value 0xa = 10. Now I will show you what the comptuer actually sees.
It's actually one long line of bits, but I have formatted it to fit here.
01100001011000100110001101100100011001010110011001100111011010000110100 10110101001101011011011000110111001101110011011110111000001110001011100 10011100110111010001110101011101100111011101111000011110010111101000001 01001000001010000100100001101000100010001010100011001000111010010000100 10010100101001001011010011000100111001001110010011110101000001010001010 10010010100110101010001010101010101100101011101011000010110010101101000 00101000110000001100010011001000110011001101000011010100110110001101110 01110000011100100100001010000000010001100100100001001010101111000100110 00101010001010000010100101011111001011010010101100111101001111000011111 00000101011111111
If you are observant, you will see I added a final byte: 11111111; this is the end of file byte.
Colors
Every color is actually a 24 bit integer. The first byte is the red byte, the next the green, and the last the blue.
Experiment with the color app on today's page to see this in action.