Understanding Hexadecimal and Binary in Python for MAC Address
400 likes | 498 Views
Learn about converting decimal to hexadecimal, binary-to-decimal conversion, base-16 system, and the significance of Base Two in Python programming for MAC addresses. Explore how modern numbering systems work with examples and practical exercises.
Understanding Hexadecimal and Binary in Python for MAC Address
E N D
Presentation Transcript
Lab #1 Follow-Up Unix Binary / Hexadecimal Python
Lab #1 Follow-Up Unix Binary / Hexadecimal Python
DecimalHex(adecimal) 0 0 11 2 2 3 3 44 5 5 6 6 7 7 88 99 10A 11 B 12C 13D 14 E 15F
DecimalHex(adecimal) 0 0 11 2 2 3 3 44 5 5 6 6 7 7 88 99 10A 11 B 12C 13D 14 E 15F B 4
DecimalHex(adecimal) 0 0 11 2 2 3 3 44 5 5 6 6 7 7 88 99 10A 11 B 12C 13D 14 E 15F B 4 11×16 + 4×1
DecimalHex(adecimal) 0 0 11 2 2 3 3 44 5 5 6 6 7 7 88 99 10A 11 B 12C 13D 14 E 15F B 4 11×161 + 4×160
DecimalHex(adecimal) 0 0 11 2 2 3 3 44 5 5 6 6 7 7 88 99 10A 11 B 12C 13D 14 E 15F B 4 11×16 + 4×1
DecimalHex(adecimal) 0 0 11 2 2 3 3 44 5 5 6 6 7 7 88 99 10A 11 B 12C 13D 14 E 15F B 4 11×16 + 4×1 176 + 4
DecimalHex(adecimal) 0 0 11 2 2 3 3 44 5 5 6 6 7 7 88 99 10A 11 B 12C 13D 14 E 15F B 4 11×16 + 4×1 176 + 4 180
All modern numbering systems work this way B 4 1 8 0 11×161 + 4×160 1×102 + 8×101 + 0×100 11×16 + 4×1 1×100 + 8×10+ 0×1 176 + 4 100 + 80 + 0 180 180
Why Base 16? DecimalHexBinary 0 0 0 11 1 22 10 33 11 44 100 55 101 66 110 77 111 88 1000 99 1001 10A 1010 11 B 1011 12C 1100 13D 1101 14 E 1110 15F 1111
Why Base 16? DecimalHexBinary 00 0 0000 011 0001 022 0010 033 0011 044 0100 055 0101 066 0110 077 0111 088 1000 099 1001 10A 1010 11 B 1011 12C 1100 13D 1101 14 E 1110 15F 1111
Eight Bits = One Byte DecimalHexBinary 0 0 0000 11 0001 22 0010 33 0011 44 0100 55 0101 66 0110 7 7 0111 88 1000 99 1001 10A 1010 11 B 1011 12C 1100 13D 1101 14 E 1110 15F 1111 B 4 1011 0100
Faster, Cheaper, Smaller Relay Transistor Vacuum tube 1940s 1950s Integrated Circuits 1960s Today
Binary-to-Decimal Conversion • To convert from binary to decimal • Start from right • Multiply 0,1 by powers of two (1, 2, 4, 8, …) • Sum of these products is decimal equivalent • E.g., 1 1 0 1 2 = ??? 10
1 * 20 = 1 • E.g., 1 1 0 12 = ??? 10 Binary-to-Decimal Conversion • To convert from binary to decimal • Start from right • Multiply 0,1 by powers of two (1, 2, 4, 8, …) • Sum of these products is decimal equivalent
1 * 20 = 1 + 0 * 21 = 0 • E.g., 1 1 0 1 2 = ??? 10 Binary-to-Decimal Conversion • To convert from binary to decimal • Start from right • Multiply 0,1 by powers of two (1, 2, 4, 8, …) • Sum of these products is decimal equivalent
1 * 20 = 1 + 0 * 21 = 0 + 1 * 22 = 4 • E.g., 1 1 0 1 2 = ??? 10 Binary-to-Decimal Conversion • To convert from binary to decimal • Start from right • Multiply 0,1 by powers of two (1, 2, 4, 8, …) • Sum of these products is decimal equivalent
1 * 20 = 1 + 0 * 21 = 0 + 1 * 22 = 4 + 1 * 23 = 8 • E.g.,1 1 0 1 2 = ??? 10 Binary-to-Decimal Conversion • To convert from binary to decimal • Start from right • Multiply 0,1 by powers of two (1, 2, 4, 8, …) • Sum of these products is decimal equivalent
1 * 20 = 1 + 0 * 21 = 0 + 1 * 22 = 4 + 1 * 23 = 8 ____________ 13 • E.g., 1 1 0 1 2 = 1310 Binary-to-Decimal Conversion • To convert from binary to decimal • Start from right • Multiply 0,1 by powers of two (1, 2, 4, 8, …) • Sum of these products is decimal equivalent
Decimal-to-Binary Conversion To convert from decimal to binary • Take remainder of decimal number / 2 • Write down remainder right-to-left • If decimal number is zero, we’re done • Divide decimal number by 2 • Go to step 1. 13 r 2 = 1 13 ÷ 2 = 6 6 r 2 = 0 6 ÷ 2 = 3 3 r 2 = 1 3 ÷ 2 = 1 1 r 2 = 1 1 ÷ 2 = 0 ___________ 1 1 0 1
Fractions 3.2510 = ????2
Fractions 3. 2 5 3×100 + 2×10-1 + 5×10-2
Fractions 3. 2 5 10 = 1 1. 0 1 2 1×21 + 1×20 + 0×2-1 + 1×2-2 3×100 + 2×10-1 + 5×10-2
Problem! 3.210 = ????2
Google patriot missile failure for a real-world example
What about text? ASCII: One byte per character
What about text? Unicode: (Up to) two bytes per character
Numbers or text? • Each application (MS Word, Excel) expects either (ASCII) text or (“raw binary”) numbers • Try opening a an Excel spreadsheet in WordPad!