Slide 1

Slide 1 text

@jessewilson Decoding the Secrets of Binary Data https://github.com/swankjesse/encoding

Slide 2

Slide 2 text

Encoding @jessewilson https://github.com/swankjesse/encoding

Slide 3

Slide 3 text

0 1

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Goals • Get comfortable with bytes • Write simpler, more efficient programs

Slide 7

Slide 7 text

Plan • Bottom up • History • Math • Code

Slide 8

Slide 8 text

1095 AD

Slide 9

Slide 9 text

Date: Aprilis 11, 1095 From: Emperor Alexios Komnenos To: Robert II, Count of Flanders Bob, Looks like we’re gonna war with the Turks. Could you see if the Pope could help us crusade against ’em? Thanks! Alex Letters! • Human readable • Signed

Slide 10

Slide 10 text

Date: Aprilis 11, 1095 From: Emperor Alexios Komnenos To: Robert II, Count of Flanders Bob, Looks like we’re gonna war with the Turks. Could you see if the Pope could help us crusade against ’em? Thanks! Alex Letters! • Slow, dangerous to transmit • Awkward to store • Requires Literacy!

Slide 11

Slide 11 text

1854

Slide 12

Slide 12 text

Telegraphs NYC SF

Slide 13

Slide 13 text

Telegraphs NYC SF

Slide 14

Slide 14 text

Morse Code A • ▬ G ▬ ▬ • M ▬ ▬ S • • • Y ▬ • ▬ ▬ 4 • • • • ▬ B ▬ • • • H • • • • N ▬ • T ▬ Z ▬ ▬ • • 5 • • • • • C ▬ • ▬ • I • • O ▬ ▬ ▬ U • • ▬ 0 ▬ ▬ ▬ ▬ ▬ 6 ▬ • • • • D ▬ • • J • ▬ ▬ ▬ P • ▬ ▬ • V • • • ▬ 1 • ▬ ▬ ▬ ▬ 7 ▬ ▬ • • • E • K ▬ • ▬ Q ▬ ▬ • ▬ W • ▬ ▬ 2 • • ▬ ▬ ▬ 8 ▬ ▬ ▬ • • F • • ▬ • L • ▬ • • R • ▬ • X ▬ • • ▬ 3 • • • ▬ ▬ 9 ▬ ▬ ▬ ▬ •

Slide 15

Slide 15 text

A • ▬ G ▬ ▬ • M ▬ ▬ S • • • Y ▬ • ▬ ▬ 4 • • • • ▬ B ▬ • • • H • • • • N ▬ • T ▬ Z ▬ ▬ • • 5 • • • • • C ▬ • ▬ • I • • O ▬ ▬ ▬ U • • ▬ 0 ▬ ▬ ▬ ▬ ▬ 6 ▬ • • • • D ▬ • • J • ▬ ▬ ▬ P • ▬ ▬ • V • • • ▬ 1 • ▬ ▬ ▬ ▬ 7 ▬ ▬ • • • E • K ▬ • ▬ Q ▬ ▬ • ▬ W • ▬ ▬ 2 • • ▬ ▬ ▬ 8 ▬ ▬ ▬ • • F • • ▬ • L • ▬ • • R • ▬ • X ▬ • • ▬ 3 • • • ▬ ▬ 9 ▬ ▬ ▬ ▬ • ▬ • •

Slide 16

Slide 16 text

A • ▬ G ▬ ▬ • M ▬ ▬ S • • • Y ▬ • ▬ ▬ 4 • • • • ▬ B ▬ • • • H • • • • N ▬ • T ▬ Z ▬ ▬ • • 5 • • • • • C ▬ • ▬ • I • • O ▬ ▬ ▬ U • • ▬ 0 ▬ ▬ ▬ ▬ ▬ 6 ▬ • • • • D ▬ • • J • ▬ ▬ ▬ P • ▬ ▬ • V • • • ▬ 1 • ▬ ▬ ▬ ▬ 7 ▬ ▬ • • • E • K ▬ • ▬ Q ▬ ▬ • ▬ W • ▬ ▬ 2 • • ▬ ▬ ▬ 8 ▬ ▬ ▬ • • F • • ▬ • L • ▬ • • R • ▬ • X ▬ • • ▬ 3 • • • ▬ ▬ 9 ▬ ▬ ▬ ▬ • ▬ • • ▬ ▬ ▬

Slide 17

Slide 17 text

A • ▬ G ▬ ▬ • M ▬ ▬ S • • • Y ▬ • ▬ ▬ 4 • • • • ▬ B ▬ • • • H • • • • N ▬ • T ▬ Z ▬ ▬ • • 5 • • • • • C ▬ • ▬ • I • • O ▬ ▬ ▬ U • • ▬ 0 ▬ ▬ ▬ ▬ ▬ 6 ▬ • • • • D ▬ • • J • ▬ ▬ ▬ P • ▬ ▬ • V • • • ▬ 1 • ▬ ▬ ▬ ▬ 7 ▬ ▬ • • • E • K ▬ • ▬ Q ▬ ▬ • ▬ W • ▬ ▬ 2 • • ▬ ▬ ▬ 8 ▬ ▬ ▬ • • F • • ▬ • L • ▬ • • R • ▬ • X ▬ • • ▬ 3 • • • ▬ ▬ 9 ▬ ▬ ▬ ▬ • ▬ • • ▬ ▬ ▬ ▬ •

Slide 18

Slide 18 text

A • ▬ G ▬ ▬ • M ▬ ▬ S • • • Y ▬ • ▬ ▬ 4 • • • • ▬ B ▬ • • • H • • • • N ▬ • T ▬ Z ▬ ▬ • • 5 • • • • • C ▬ • ▬ • I • • O ▬ ▬ ▬ U • • ▬ 0 ▬ ▬ ▬ ▬ ▬ 6 ▬ • • • • D ▬ • • J • ▬ ▬ ▬ P • ▬ ▬ • V • • • ▬ 1 • ▬ ▬ ▬ ▬ 7 ▬ ▬ • • • E • K ▬ • ▬ Q ▬ ▬ • ▬ W • ▬ ▬ 2 • • ▬ ▬ ▬ 8 ▬ ▬ ▬ • • F • • ▬ • L • ▬ • • R • ▬ • X ▬ • • ▬ 3 • • • ▬ ▬ 9 ▬ ▬ ▬ ▬ • ▬ • • ▬ ▬ ▬ ▬ • • • ▬

Slide 19

Slide 19 text

A • ▬ G ▬ ▬ • M ▬ ▬ S • • • Y ▬ • ▬ ▬ 4 • • • • ▬ B ▬ • • • H • • • • N ▬ • T ▬ Z ▬ ▬ • • 5 • • • • • C ▬ • ▬ • I • • O ▬ ▬ ▬ U • • ▬ 0 ▬ ▬ ▬ ▬ ▬ 6 ▬ • • • • D ▬ • • J • ▬ ▬ ▬ P • ▬ ▬ • V • • • ▬ 1 • ▬ ▬ ▬ ▬ 7 ▬ ▬ • • • E • K ▬ • ▬ Q ▬ ▬ • ▬ W • ▬ ▬ 2 • • ▬ ▬ ▬ 8 ▬ ▬ ▬ • • F • • ▬ • L • ▬ • • R • ▬ • X ▬ • • ▬ 3 • • • ▬ ▬ 9 ▬ ▬ ▬ ▬ • ▬ • • ▬ ▬ ▬ ▬ • • • ▬ ▬

Slide 20

Slide 20 text

A • ▬ G ▬ ▬ • M ▬ ▬ S • • • Y ▬ • ▬ ▬ 4 • • • • ▬ B ▬ • • • H • • • • N ▬ • T ▬ Z ▬ ▬ • • 5 • • • • • C ▬ • ▬ • I • • O ▬ ▬ ▬ U • • ▬ 0 ▬ ▬ ▬ ▬ ▬ 6 ▬ • • • • D ▬ • • J • ▬ ▬ ▬ P • ▬ ▬ • V • • • ▬ 1 • ▬ ▬ ▬ ▬ 7 ▬ ▬ • • • E • K ▬ • ▬ Q ▬ ▬ • ▬ W • ▬ ▬ 2 • • ▬ ▬ ▬ 8 ▬ ▬ ▬ • • F • • ▬ • L • ▬ • • R • ▬ • X ▬ • • ▬ 3 • • • ▬ ▬ 9 ▬ ▬ ▬ ▬ • ▬ • • ▬ ▬ ▬ ▬ • • • ▬ ▬

Slide 21

Slide 21 text

Morse Code NYC SF

Slide 22

Slide 22 text

Morse Code • Telegraphs! • 15 words per minute • Wireless in 1898

Slide 23

Slide 23 text

Morse Code • Very limited characters • No lowercase • Limited punctuation* • Average message: 12 words * For example, morse code doesn’t have an asterisk symbol.

Slide 24

Slide 24 text

Binary Refresher!

Slide 25

Slide 25 text

205

Slide 26

Slide 26 text

200 205 = + 5

Slide 27

Slide 27 text

2×100 205 = + 5×1

Slide 28

Slide 28 text

2×100 205 = + + 0×10 5×1

Slide 29

Slide 29 text

2×102 205 = + + 0×101 5×10

Slide 30

Slide 30 text

205

Slide 31

Slide 31 text

205

Slide 32

Slide 32 text

128 8 + 205 = 64 4 + + + 1

Slide 33

Slide 33 text

1×128 1×8 + 205 = 1×64 1×4 + + + 1×1

Slide 34

Slide 34 text

1×128 1×8 + 205 1×64 + 1×4 + 0×32 0×16 + + 0×2 + 1×1 + =

Slide 35

Slide 35 text

1×27 1×23 + 205 1×26 + 1×22 + 0×25 0×24 + + 0×21 + 1×20 + =

Slide 36

Slide 36 text

205 1×27 1×23 1×26 1×22 0×25 0×24 0×21 1×20 =

Slide 37

Slide 37 text

205 1×27 1×23 1×26 1×22 0×25 0×240×21 1×20 0b =

Slide 38

Slide 38 text

Binary Refresher! • Decimal lets you represent any integer with a sequence of digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 • Binary lets you represent any integer with a sequence of bits: 0, 1

Slide 39

Slide 39 text

Binary Refresher! • Typically prefixed with “0b”, like 0b11001101

Slide 40

Slide 40 text

Binary Refresher! • N bits can represent 2N values • 8 bits: 0..255 • 16 bits: 0..65,535 • 32 bits: 0..4,294,967,295 • 64 bits: 0..18,446,744,073,709,551,615

Slide 41

Slide 41 text

Binary Refresher! NYC SF

Slide 42

Slide 42 text

Layering • Given a wire that transmits a single bit, we can use binary to encode any integer! • This works because the sender and recipient agree on how to interpret the sequence • That interpretation is called an encoding

Slide 43

Slide 43 text

Bytes • Though binary supports any number of bits, we like 8-bit integers • An 8-bit integer is called a byte • 256 values from 0 to 255

Slide 44

Slide 44 text

1967

Slide 45

Slide 45 text

ASCII • American Standard Code for Information Interchange • A table of characters • Interpret a sequence of bytes as a string of characters!

Slide 46

Slide 46 text

ASCII • Work started in 1960 • Only uses 7 bits: in 1967 bits were very expensive! • That means there’s 27 = 128 characters

Slide 47

Slide 47 text

ASCII 0 NULL 16 DLE 32 SP 48 0 64 @ 80 P 96 ` 112 p 1 SOH 17 DC1 33 ! 49 1 65 A 81 Q 97 a 113 q 2 STX 18 DC2 34 " 50 2 66 B 82 R 98 b 114 r 3 ETX 19 DC3 35 # 51 3 67 C 83 S 99 c 115 s 4 EOT 20 DC4 36 $ 52 4 68 D 84 T 100 d 116 t 5 ENQ 21 NAK 37 % 53 5 69 E 85 U 101 e 117 u 6 ACK 22 SYN 38 & 54 6 70 F 86 V 102 f 118 v 7 BEL 23 ETB 39 ' 55 7 71 G 87 W 103 g 119 w 8 BS 24 CAN 40 ( 56 8 72 H 88 X 104 h 120 x 9 HT 25 EM 41 ) 57 9 73 I 89 Y 105 i 121 y 10 LF 26 SUB 42 * 58 : 74 J 90 Z 106 j 122 z 11 VT 27 ESC 43 + 59 ; 75 K 91 [ 107 k 123 { 12 FF 28 FS 44 , 60 < 76 L 92 \ 108 l 124 | 13 CR 29 GS 45 - 61 = 77 M 93 ] 109 m 125 } 14 SO 30 RS 46 . 62 > 78 N 94 ^ 110 n 126 ~ 15 SI 31 US 47 / 63 ? 79 O 95 _ 111 o 127 DEL

Slide 48

Slide 48 text

0 NULL 16 DLE 32 SP 48 0 64 @ 80 P 96 ` 112 p 1 SOH 17 DC1 33 ! 49 1 65 A 81 Q 97 a 113 q 2 STX 18 DC2 34 " 50 2 66 B 82 R 98 b 114 r 3 ETX 19 DC3 35 # 51 3 67 C 83 S 99 c 115 s 4 EOT 20 DC4 36 $ 52 4 68 D 84 T 100 d 116 t 5 ENQ 21 NAK 37 % 53 5 69 E 85 U 101 e 117 u 6 ACK 22 SYN 38 & 54 6 70 F 86 V 102 f 118 v 7 BEL 23 ETB 39 ' 55 7 71 G 87 W 103 g 119 w 8 BS 24 CAN 40 ( 56 8 72 H 88 X 104 h 120 x 9 HT 25 EM 41 ) 57 9 73 I 89 Y 105 i 121 y 10 LF 26 SUB 42 * 58 : 74 J 90 Z 106 j 122 z 11 VT 27 ESC 43 + 59 ; 75 K 91 [ 107 k 123 { 12 FF 28 FS 44 , 60 < 76 L 92 \ 108 l 124 | 13 CR 29 GS 45 - 61 = 77 M 93 ] 109 m 125 } 14 SO 30 RS 46 . 62 > 78 N 94 ^ 110 n 126 ~ 15 SI 31 US 47 / 63 ? 79 O 95 _ 111 o 127 DEL 68

Slide 49

Slide 49 text

0 NULL 16 DLE 32 SP 48 0 64 @ 80 P 96 ` 112 p 1 SOH 17 DC1 33 ! 49 1 65 A 81 Q 97 a 113 q 2 STX 18 DC2 34 " 50 2 66 B 82 R 98 b 114 r 3 ETX 19 DC3 35 # 51 3 67 C 83 S 99 c 115 s 4 EOT 20 DC4 36 $ 52 4 68 D 84 T 100 d 116 t 5 ENQ 21 NAK 37 % 53 5 69 E 85 U 101 e 117 u 6 ACK 22 SYN 38 & 54 6 70 F 86 V 102 f 118 v 7 BEL 23 ETB 39 ' 55 7 71 G 87 W 103 g 119 w 8 BS 24 CAN 40 ( 56 8 72 H 88 X 104 h 120 x 9 HT 25 EM 41 ) 57 9 73 I 89 Y 105 i 121 y 10 LF 26 SUB 42 * 58 : 74 J 90 Z 106 j 122 z 11 VT 27 ESC 43 + 59 ; 75 K 91 [ 107 k 123 { 12 FF 28 FS 44 , 60 < 76 L 92 \ 108 l 124 | 13 CR 29 GS 45 - 61 = 77 M 93 ] 109 m 125 } 14 SO 30 RS 46 . 62 > 78 N 94 ^ 110 n 126 ~ 15 SI 31 US 47 / 63 ? 79 O 95 _ 111 o 127 DEL 68 111

Slide 50

Slide 50 text

0 NULL 16 DLE 32 SP 48 0 64 @ 80 P 96 ` 112 p 1 SOH 17 DC1 33 ! 49 1 65 A 81 Q 97 a 113 q 2 STX 18 DC2 34 " 50 2 66 B 82 R 98 b 114 r 3 ETX 19 DC3 35 # 51 3 67 C 83 S 99 c 115 s 4 EOT 20 DC4 36 $ 52 4 68 D 84 T 100 d 116 t 5 ENQ 21 NAK 37 % 53 5 69 E 85 U 101 e 117 u 6 ACK 22 SYN 38 & 54 6 70 F 86 V 102 f 118 v 7 BEL 23 ETB 39 ' 55 7 71 G 87 W 103 g 119 w 8 BS 24 CAN 40 ( 56 8 72 H 88 X 104 h 120 x 9 HT 25 EM 41 ) 57 9 73 I 89 Y 105 i 121 y 10 LF 26 SUB 42 * 58 : 74 J 90 Z 106 j 122 z 11 VT 27 ESC 43 + 59 ; 75 K 91 [ 107 k 123 { 12 FF 28 FS 44 , 60 < 76 L 92 \ 108 l 124 | 13 CR 29 GS 45 - 61 = 77 M 93 ] 109 m 125 } 14 SO 30 RS 46 . 62 > 78 N 94 ^ 110 n 126 ~ 15 SI 31 US 47 / 63 ? 79 O 95 _ 111 o 127 DEL 68 111 110

Slide 51

Slide 51 text

0 NULL 16 DLE 32 SP 48 0 64 @ 80 P 96 ` 112 p 1 SOH 17 DC1 33 ! 49 1 65 A 81 Q 97 a 113 q 2 STX 18 DC2 34 " 50 2 66 B 82 R 98 b 114 r 3 ETX 19 DC3 35 # 51 3 67 C 83 S 99 c 115 s 4 EOT 20 DC4 36 $ 52 4 68 D 84 T 100 d 116 t 5 ENQ 21 NAK 37 % 53 5 69 E 85 U 101 e 117 u 6 ACK 22 SYN 38 & 54 6 70 F 86 V 102 f 118 v 7 BEL 23 ETB 39 ' 55 7 71 G 87 W 103 g 119 w 8 BS 24 CAN 40 ( 56 8 72 H 88 X 104 h 120 x 9 HT 25 EM 41 ) 57 9 73 I 89 Y 105 i 121 y 10 LF 26 SUB 42 * 58 : 74 J 90 Z 106 j 122 z 11 VT 27 ESC 43 + 59 ; 75 K 91 [ 107 k 123 { 12 FF 28 FS 44 , 60 < 76 L 92 \ 108 l 124 | 13 CR 29 GS 45 - 61 = 77 M 93 ] 109 m 125 } 14 SO 30 RS 46 . 62 > 78 N 94 ^ 110 n 126 ~ 15 SI 31 US 47 / 63 ? 79 O 95 _ 111 o 127 DEL 68 111 110 117

Slide 52

Slide 52 text

0 NULL 16 DLE 32 SP 48 0 64 @ 80 P 96 ` 112 p 1 SOH 17 DC1 33 ! 49 1 65 A 81 Q 97 a 113 q 2 STX 18 DC2 34 " 50 2 66 B 82 R 98 b 114 r 3 ETX 19 DC3 35 # 51 3 67 C 83 S 99 c 115 s 4 EOT 20 DC4 36 $ 52 4 68 D 84 T 100 d 116 t 5 ENQ 21 NAK 37 % 53 5 69 E 85 U 101 e 117 u 6 ACK 22 SYN 38 & 54 6 70 F 86 V 102 f 118 v 7 BEL 23 ETB 39 ' 55 7 71 G 87 W 103 g 119 w 8 BS 24 CAN 40 ( 56 8 72 H 88 X 104 h 120 x 9 HT 25 EM 41 ) 57 9 73 I 89 Y 105 i 121 y 10 LF 26 SUB 42 * 58 : 74 J 90 Z 106 j 122 z 11 VT 27 ESC 43 + 59 ; 75 K 91 [ 107 k 123 { 12 FF 28 FS 44 , 60 < 76 L 92 \ 108 l 124 | 13 CR 29 GS 45 - 61 = 77 M 93 ] 109 m 125 } 14 SO 30 RS 46 . 62 > 78 N 94 ^ 110 n 126 ~ 15 SI 31 US 47 / 63 ? 79 O 95 _ 111 o 127 DEL 68 111 110 117 116

Slide 53

Slide 53 text

0 NULL 16 DLE 32 SP 48 0 64 @ 80 P 96 ` 112 p 1 SOH 17 DC1 33 ! 49 1 65 A 81 Q 97 a 113 q 2 STX 18 DC2 34 " 50 2 66 B 82 R 98 b 114 r 3 ETX 19 DC3 35 # 51 3 67 C 83 S 99 c 115 s 4 EOT 20 DC4 36 $ 52 4 68 D 84 T 100 d 116 t 5 ENQ 21 NAK 37 % 53 5 69 E 85 U 101 e 117 u 6 ACK 22 SYN 38 & 54 6 70 F 86 V 102 f 118 v 7 BEL 23 ETB 39 ' 55 7 71 G 87 W 103 g 119 w 8 BS 24 CAN 40 ( 56 8 72 H 88 X 104 h 120 x 9 HT 25 EM 41 ) 57 9 73 I 89 Y 105 i 121 y 10 LF 26 SUB 42 * 58 : 74 J 90 Z 106 j 122 z 11 VT 27 ESC 43 + 59 ; 75 K 91 [ 107 k 123 { 12 FF 28 FS 44 , 60 < 76 L 92 \ 108 l 124 | 13 CR 29 GS 45 - 61 = 77 M 93 ] 109 m 125 } 14 SO 30 RS 46 . 62 > 78 N 94 ^ 110 n 126 ~ 15 SI 31 US 47 / 63 ? 79 O 95 _ 111 o 127 DEL 68 111 110 117 116

Slide 54

Slide 54 text

0 NULL 16 DLE 32 SP 48 0 64 @ 80 P 96 ` 112 p 1 SOH 17 DC1 33 ! 49 1 65 A 81 Q 97 a 113 q 2 STX 18 DC2 34 " 50 2 66 B 82 R 98 b 114 r 3 ETX 19 DC3 35 # 51 3 67 C 83 S 99 c 115 s 4 EOT 20 DC4 36 $ 52 4 68 D 84 T 100 d 116 t 5 ENQ 21 NAK 37 % 53 5 69 E 85 U 101 e 117 u 6 ACK 22 SYN 38 & 54 6 70 F 86 V 102 f 118 v 7 BEL 23 ETB 39 ' 55 7 71 G 87 W 103 g 119 w 8 BS 24 CAN 40 ( 56 8 72 H 88 X 104 h 120 x 9 HT 25 EM 41 ) 57 9 73 I 89 Y 105 i 121 y 10 LF 26 SUB 42 * 58 : 74 J 90 Z 106 j 122 z 11 VT 27 ESC 43 + 59 ; 75 K 91 [ 107 k 123 { 12 FF 28 FS 44 , 60 < 76 L 92 \ 108 l 124 | 13 CR 29 GS 45 - 61 = 77 M 93 ] 109 m 125 } 14 SO 30 RS 46 . 62 > 78 N 94 ^ 110 n 126 ~ 15 SI 31 US 47 / 63 ? 79 O 95 _ 111 o 127 DEL 68 111 110 117 116

Slide 55

Slide 55 text

0 NULL 16 DLE 32 SP 48 0 64 @ 80 P 96 ` 112 p 1 SOH 17 DC1 33 ! 49 1 65 A 81 Q 97 a 113 q 2 STX 18 DC2 34 " 50 2 66 B 82 R 98 b 114 r 3 ETX 19 DC3 35 # 51 3 67 C 83 S 99 c 115 s 4 EOT 20 DC4 36 $ 52 4 68 D 84 T 100 d 116 t 5 ENQ 21 NAK 37 % 53 5 69 E 85 U 101 e 117 u 6 ACK 22 SYN 38 & 54 6 70 F 86 V 102 f 118 v 7 BEL 23 ETB 39 ' 55 7 71 G 87 W 103 g 119 w 8 BS 24 CAN 40 ( 56 8 72 H 88 X 104 h 120 x 9 HT 25 EM 41 ) 57 9 73 I 89 Y 105 i 121 y 10 LF 26 SUB 42 * 58 : 74 J 90 Z 106 j 122 z 11 VT 27 ESC 43 + 59 ; 75 K 91 [ 107 k 123 { 12 FF 28 FS 44 , 60 < 76 L 92 \ 108 l 124 | 13 CR 29 GS 45 - 61 = 77 M 93 ] 109 m 125 } 14 SO 30 RS 46 . 62 > 78 N 94 ^ 110 n 126 ~ 15 SI 31 US 47 / 63 ? 79 O 95 _ 111 o 127 DEL 111 110 117 116 0 1 0 0 0 1 0 0

Slide 56

Slide 56 text

0 NULL 16 DLE 32 SP 48 0 64 @ 80 P 96 ` 112 p 1 SOH 17 DC1 33 ! 49 1 65 A 81 Q 97 a 113 q 2 STX 18 DC2 34 " 50 2 66 B 82 R 98 b 114 r 3 ETX 19 DC3 35 # 51 3 67 C 83 S 99 c 115 s 4 EOT 20 DC4 36 $ 52 4 68 D 84 T 100 d 116 t 5 ENQ 21 NAK 37 % 53 5 69 E 85 U 101 e 117 u 6 ACK 22 SYN 38 & 54 6 70 F 86 V 102 f 118 v 7 BEL 23 ETB 39 ' 55 7 71 G 87 W 103 g 119 w 8 BS 24 CAN 40 ( 56 8 72 H 88 X 104 h 120 x 9 HT 25 EM 41 ) 57 9 73 I 89 Y 105 i 121 y 10 LF 26 SUB 42 * 58 : 74 J 90 Z 106 j 122 z 11 VT 27 ESC 43 + 59 ; 75 K 91 [ 107 k 123 { 12 FF 28 FS 44 , 60 < 76 L 92 \ 108 l 124 | 13 CR 29 GS 45 - 61 = 77 M 93 ] 109 m 125 } 14 SO 30 RS 46 . 62 > 78 N 94 ^ 110 n 126 ~ 15 SI 31 US 47 / 63 ? 79 O 95 _ 111 o 127 DEL 110 117 116 0 1 0 0 0 1 0 0 0 1 1 0 1 1 1 1

Slide 57

Slide 57 text

0 NULL 16 DLE 32 SP 48 0 64 @ 80 P 96 ` 112 p 1 SOH 17 DC1 33 ! 49 1 65 A 81 Q 97 a 113 q 2 STX 18 DC2 34 " 50 2 66 B 82 R 98 b 114 r 3 ETX 19 DC3 35 # 51 3 67 C 83 S 99 c 115 s 4 EOT 20 DC4 36 $ 52 4 68 D 84 T 100 d 116 t 5 ENQ 21 NAK 37 % 53 5 69 E 85 U 101 e 117 u 6 ACK 22 SYN 38 & 54 6 70 F 86 V 102 f 118 v 7 BEL 23 ETB 39 ' 55 7 71 G 87 W 103 g 119 w 8 BS 24 CAN 40 ( 56 8 72 H 88 X 104 h 120 x 9 HT 25 EM 41 ) 57 9 73 I 89 Y 105 i 121 y 10 LF 26 SUB 42 * 58 : 74 J 90 Z 106 j 122 z 11 VT 27 ESC 43 + 59 ; 75 K 91 [ 107 k 123 { 12 FF 28 FS 44 , 60 < 76 L 92 \ 108 l 124 | 13 CR 29 GS 45 - 61 = 77 M 93 ] 109 m 125 } 14 SO 30 RS 46 . 62 > 78 N 94 ^ 110 n 126 ~ 15 SI 31 US 47 / 63 ? 79 O 95 _ 111 o 127 DEL 117 116 0 1 0 0 0 1 0 0 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 0

Slide 58

Slide 58 text

0 NULL 16 DLE 32 SP 48 0 64 @ 80 P 96 ` 112 p 1 SOH 17 DC1 33 ! 49 1 65 A 81 Q 97 a 113 q 2 STX 18 DC2 34 " 50 2 66 B 82 R 98 b 114 r 3 ETX 19 DC3 35 # 51 3 67 C 83 S 99 c 115 s 4 EOT 20 DC4 36 $ 52 4 68 D 84 T 100 d 116 t 5 ENQ 21 NAK 37 % 53 5 69 E 85 U 101 e 117 u 6 ACK 22 SYN 38 & 54 6 70 F 86 V 102 f 118 v 7 BEL 23 ETB 39 ' 55 7 71 G 87 W 103 g 119 w 8 BS 24 CAN 40 ( 56 8 72 H 88 X 104 h 120 x 9 HT 25 EM 41 ) 57 9 73 I 89 Y 105 i 121 y 10 LF 26 SUB 42 * 58 : 74 J 90 Z 106 j 122 z 11 VT 27 ESC 43 + 59 ; 75 K 91 [ 107 k 123 { 12 FF 28 FS 44 , 60 < 76 L 92 \ 108 l 124 | 13 CR 29 GS 45 - 61 = 77 M 93 ] 109 m 125 } 14 SO 30 RS 46 . 62 > 78 N 94 ^ 110 n 126 ~ 15 SI 31 US 47 / 63 ? 79 O 95 _ 111 o 127 DEL 116 0 1 0 0 0 1 0 0 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 0 0 1 1 1 0 1 0 1

Slide 59

Slide 59 text

0 NULL 16 DLE 32 SP 48 0 64 @ 80 P 96 ` 112 p 1 SOH 17 DC1 33 ! 49 1 65 A 81 Q 97 a 113 q 2 STX 18 DC2 34 " 50 2 66 B 82 R 98 b 114 r 3 ETX 19 DC3 35 # 51 3 67 C 83 S 99 c 115 s 4 EOT 20 DC4 36 $ 52 4 68 D 84 T 100 d 116 t 5 ENQ 21 NAK 37 % 53 5 69 E 85 U 101 e 117 u 6 ACK 22 SYN 38 & 54 6 70 F 86 V 102 f 118 v 7 BEL 23 ETB 39 ' 55 7 71 G 87 W 103 g 119 w 8 BS 24 CAN 40 ( 56 8 72 H 88 X 104 h 120 x 9 HT 25 EM 41 ) 57 9 73 I 89 Y 105 i 121 y 10 LF 26 SUB 42 * 58 : 74 J 90 Z 106 j 122 z 11 VT 27 ESC 43 + 59 ; 75 K 91 [ 107 k 123 { 12 FF 28 FS 44 , 60 < 76 L 92 \ 108 l 124 | 13 CR 29 GS 45 - 61 = 77 M 93 ] 109 m 125 } 14 SO 30 RS 46 . 62 > 78 N 94 ^ 110 n 126 ~ 15 SI 31 US 47 / 63 ? 79 O 95 _ 111 o 127 DEL 0 1 0 0 0 1 0 0 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 0 0 1 1 1 0 1 0 1 0 1 1 1 0 1 0 0

Slide 60

Slide 60 text

1980’s

Slide 61

Slide 61 text

boss, what should I be working on?

Slide 62

Slide 62 text

boss, what should I be working on? resume polishing

Slide 63

Slide 63 text

boss, what should I be working on?

Slide 64

Slide 64 text

boss, what should I be working on? résumé polishing

Slide 65

Slide 65 text

Charset Hell • ASCII is English only • But non-English people also use computers!

Slide 66

Slide 66 text

Charset Hell • Operating systems were installed for a specific character set and wouldn’t work with any others • Documents couldn’t mix Greek, French, and Russian characters • If you see ISO-8859-1, run away!

Slide 67

Slide 67 text

1991

Slide 68

Slide 68 text

Unicode • Support all languages in a single system • A code point is a universal ID for a character

Slide 69

Slide 69 text

UTF-16 • 16-bit Unicode Transformation Format • 2 bytes per code point • This is Java’s char type

Slide 70

Slide 70 text

ASCII UTF-16

Slide 71

Slide 71 text

ASCII 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 D UTF-16

Slide 72

Slide 72 text

ASCII 0 1 0 0 0 1 0 0 0 1 1 0 1 1 1 1 0 1 0 0 0 1 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 D UTF-16 o

Slide 73

Slide 73 text

ASCII 0 1 0 0 0 1 0 0 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 0 0 1 0 0 0 1 0 0 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 D UTF-16 on

Slide 74

Slide 74 text

ASCII 0 1 0 0 0 1 0 0 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 0 0 1 1 1 0 1 0 1 0 1 0 0 0 1 0 0 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 0 0 1 1 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 D UTF-16 onu

Slide 75

Slide 75 text

ASCII 0 1 0 0 0 1 0 0 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 0 0 1 1 1 0 1 0 1 0 1 1 1 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 0 0 1 1 1 0 1 0 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 D UTF-16 onut

Slide 76

Slide 76 text

ASCII 0 1 0 0 0 1 0 0 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 0 0 1 1 1 0 1 0 1 0 1 1 1 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 0 0 1 1 1 0 1 0 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 D UTF-16 onut

Slide 77

Slide 77 text

UTF-16 characters • Max code point is 65,535 • Code point for is 127,849 • 127,849 > 65,535

Slide 78

Slide 78 text

Java’s char is broken! • There’s a system called “surrogate pairs” which is like multidex for code points • It splits a single code point across 2 chars • It’s an incredible pain

Slide 79

Slide 79 text


 char[] s = "Café ".toCharArray(); C a f é 0 1 2 3 4 5 6

Slide 80

Slide 80 text

String s = “Café ";
 
 for (int i = 0, size = s.length(); i < size; i++) {
 char c = s.charAt(i);
 System.out.printf("The character at %d is '%c'%n", i, c);
 }

Slide 81

Slide 81 text

String s = “Café ";
 
 for (int i = 0, size = s.length(); i < size; i++) {
 char c = s.charAt(i);
 System.out.printf("The character at %d is '%c'%n", i, c);
 } The character at 0 is 'C' The character at 1 is 'a' The character at 2 is 'f' The character at 3 is 'é' The character at 4 is ' ' The character at 5 is ' ' The character at 6 is ' '

Slide 82

Slide 82 text

String s = “Café ";
 
 for (int i = 0, size = s.length(); i < size; ) {
 int c = s.codePointAt(i);
 System.out.printf("The code point at %d is '%c'%n", i, c); i += Character.charCount(c);
 }

Slide 83

Slide 83 text

String s = “Café ";
 
 for (int i = 0, size = s.length(); i < size; ) {
 int c = s.codePointAt(i);
 System.out.printf("The code point at %d is '%c'%n", i, c); i += Character.charCount(c);
 } The code point at 0 is 'C' The code point at 1 is 'a' The code point at 2 is 'f' The code point at 3 is 'é' The code point at 4 is ' ' The code point at 5 is ' '

Slide 84

Slide 84 text

1998

Slide 85

Slide 85 text

UTF-8 • 8-bit Unicode Transformation Format • Variable number of bytes per code point • This is how modern apps transmit & store text

Slide 86

Slide 86 text

Image copyright Chris55, https://commons.wikimedia.org/wiki/File:Utf8webgrowth.svg

Slide 87

Slide 87 text

UTF-8 • Many common characters are 1-byte • Some are 2 and 3 bytes. ‘ ’ is 4 bytes. • Self-delimiting • Self-aligning

Slide 88

Slide 88 text

0 1 1 0 1 0 1 1 1 0 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 ≤7 ≤11 ≤16 ≤21 “How many bits do you need?”

Slide 89

Slide 89 text

C “How many bits do you need?” a f é sp

Slide 90

Slide 90 text

C “How many bits do you need?” a f é sp 67

Slide 91

Slide 91 text

1 0 0 0 0 1 1 C “How many bits do you need?” a f é sp

Slide 92

Slide 92 text

1 0 0 0 0 1 1 C “How many bits do you need?” a f é sp 97

Slide 93

Slide 93 text

1 0 0 0 0 1 1 C “How many bits do you need?” 1 1 0 0 0 0 1 a f é sp

Slide 94

Slide 94 text

1 0 0 0 0 1 1 C “How many bits do you need?” 1 1 0 0 0 0 1 a f é sp 102

Slide 95

Slide 95 text

1 0 0 0 0 1 1 C “How many bits do you need?” 1 1 0 0 0 0 1 a 1 1 0 0 1 1 0 f é sp

Slide 96

Slide 96 text

233 1 0 0 0 0 1 1 C “How many bits do you need?” 1 1 0 0 0 0 1 a 1 1 0 0 1 1 0 f é sp

Slide 97

Slide 97 text

1 1 1 0 1 0 0 1 1 0 0 0 0 1 1 C “How many bits do you need?” 1 1 0 0 0 0 1 a 1 1 0 0 1 1 0 f é sp

Slide 98

Slide 98 text

1 1 1 0 1 0 0 1 1 0 0 0 0 1 1 C “How many bits do you need?” 1 1 0 0 0 0 1 a 1 1 0 0 1 1 0 f é sp 32

Slide 99

Slide 99 text

1 0 0 0 0 0 1 1 1 0 1 0 0 1 1 0 0 0 0 1 1 C “How many bits do you need?” 1 1 0 0 0 0 1 a 1 1 0 0 1 1 0 f é sp

Slide 100

Slide 100 text

1 0 0 0 0 0 1 1 1 0 1 0 0 1 1 0 0 0 0 1 1 C “How many bits do you need?” 1 1 0 0 0 0 1 a 1 1 0 0 1 1 0 f é sp 127,849

Slide 101

Slide 101 text

1 0 1 0 0 1 0 0 1 1 0 1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 0 1 0 0 1 1 0 0 0 0 1 1 C “How many bits do you need?” 1 1 0 0 0 0 1 a 1 1 0 0 1 1 0 f é sp

Slide 102

Slide 102 text

0 1 1 0 1 1 1 1 0 1 0 0 1 0 0 0 1 0 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 1 1 1 1 0 0 0 0 0 1 0 1 0 0 1 C “How many bits do you need?” 1 1 0 0 0 0 1 a 1 1 0 0 1 1 0 f é sp 1 1 1 0 0 0 0 1 1

Slide 103

Slide 103 text

0 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 0 0 0 1 0 0 0 1 0 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 1 1 1 1 0 0 0 0 0 1 0 1 0 0 1 C “How many bits do you need?” 1 1 0 0 0 0 1 a 1 1 0 0 1 1 0 f é sp 1 1 1 0 0 0 0 1 1

Slide 104

Slide 104 text

0 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 0 1 0 1 0 0 1 1 1 1 1 0 0 0 0 1 0 0 1 1 1 1 1 1 0 0 0 1 1 0 1 1 0 1 0 1 0 0 1 0 1 1 0 0 0 0 1 0 1 1 0 0 1 1 0 0 0 1 0 0 0 0 0

Slide 105

Slide 105 text

UTF-8 • Basically the best thing ever • Superset of ASCII • Great for JSON and HTML because delimiter characters <, > and " are 1-byte

Slide 106

Slide 106 text

Colors

Slide 107

Slide 107 text

R G B

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

R 255 0 G 255 0 B 255 0 255 255 255

Slide 112

Slide 112 text

R 255 0 G 255 0 B 255 0 255 0 255

Slide 113

Slide 113 text

R 255 0 G 255 0 B 255 0 20 206 210

Slide 114

Slide 114 text

Hexadecimal Refresher!

Slide 115

Slide 115 text

Hexadecimal Refresher! • Decimal digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 • Binary bits: 0, 1 • Hexadecimal digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f

Slide 116

Slide 116 text

205

Slide 117

Slide 117 text

205 192 = + 13

Slide 118

Slide 118 text

205 12×16 = + 13×1

Slide 119

Slide 119 text

205 12×161 = + 13×160

Slide 120

Slide 120 text

205 c×161 = + d×160

Slide 121

Slide 121 text

205 c = d 0x

Slide 122

Slide 122 text

Hexadecimal Refresher! • Prefixed with “0x”, like 0xcd • Hex bytes are always two digits: 00, 01, 02 … ff • Sequences of bytes are okay: 0bb634

Slide 123

Slide 123 text

Hexadecimal Refresher! • Colors: #ffffff • URL escaping: http://example.com/?q=hello%20world • Unicode code points U+2020 • IPv6 addresses: 2001:0db8:85a3:0000:0000:8a2e:0370:7334

Slide 124

Slide 124 text

R 255 0 G 255 0 B 255 0 ced214

Slide 125

Slide 125 text

Pictures

Slide 126

Slide 126 text

Pictures • Just a 2D array of colors • Given 3 bytes per pixel: • 64 × 64 icon is 12,288 bytes • 1080 × 1920 picture is 5.9 MiB • Compression is important!

Slide 127

Slide 127 text

Pictures • Android can use fewer bits per pixel • ARGB_8888: alpha, red, green, and blue get 8 bits each • RGB_565: red gets 5 bits, geen gets 6, blue gets 5 bits

Slide 128

Slide 128 text

RGB_565 ARGB_8888

Slide 129

Slide 129 text

RGB_454 ARGB_8888

Slide 130

Slide 130 text

RGB_232 ARGB_8888

Slide 131

Slide 131 text

/** https://en.wikipedia.org/wiki/BMP_file_format */
 public void encode(BufferedSink sink) throws IOException {
 int height = pixels.length;
 int width = pixels[0].length;
 
 int bytesPerPixel = 3;
 int rowByteCountWithoutPadding = (bytesPerPixel * width);
 int rowByteCount = ((rowByteCountWithoutPadding + 3) / 4) * 4;
 int pixelDataSize = rowByteCount * height;
 int bmpHeaderSize = 14;
 int dibHeaderSize = 40;
 
 // BMP Header
 sink.writeUtf8("BM"); // ID.
 sink.writeIntLe(bmpHeaderSize + dibHeaderSize + pixelDataSize); // File size.
 sink.writeShortLe(0); // Unused.
 sink.writeShortLe(0); // Unused.
 sink.writeIntLe(bmpHeaderSize + dibHeaderSize); // Offset of pixel data.
 
 // DIB Header
 sink.writeIntLe(dibHeaderSize);


Slide 132

Slide 132 text

int dibHeaderSize = 40;
 
 // BMP Header
 sink.writeUtf8("BM"); // ID.
 sink.writeIntLe(bmpHeaderSize + dibHeaderSize + pixelDataSize); // File size.
 sink.writeShortLe(0); // Unused.
 sink.writeShortLe(0); // Unused.
 sink.writeIntLe(bmpHeaderSize + dibHeaderSize); // Offset of pixel data.
 
 // DIB Header
 sink.writeIntLe(dibHeaderSize);
 sink.writeIntLe(width);
 sink.writeIntLe(height);
 sink.writeShortLe(1); // Color plane count.
 sink.writeShortLe(bytesPerPixel * Byte.SIZE);
 sink.writeIntLe(0); // No compression.
 sink.writeIntLe(16); // Size of bitmap data including padding.
 sink.writeIntLe(2835); // Horizontal print resolution in pixels/meter. (72 dpi).
 sink.writeIntLe(2835); // Vertical print resolution in pixels/meter. (72 dpi).
 sink.writeIntLe(0); // Palette color count.
 sink.writeIntLe(0); // 0 important colors.
 
 // Pixel data.


Slide 133

Slide 133 text

sink.writeIntLe(0); // Palette color count.
 sink.writeIntLe(0); // 0 important colors.
 
 // Pixel data.
 for (int y = height - 1; y >= 0; y--) {
 int[] row = pixels[y];
 for (int x = 0; x < width; x++) {
 int pixel = row[x];
 sink.writeByte((pixel & 0x0000ff)); // Blue.
 sink.writeByte((pixel & 0x00ff00) >>> 8); // Green.
 sink.writeByte((pixel & 0xff0000) >>> 16); // Red.
 }
 
 // Padding for 4-byte alignment.
 for (int p = rowByteCountWithoutPadding; p < rowByteCount; p++) {
 sink.writeByte(0);
 }
 }
 }


Slide 134

Slide 134 text

Pictures on Bytes • This bitmap writer is 50 lines of code • Decoders are more difficult! • Good specs make it easy

Slide 135

Slide 135 text

sink.writeIntLe(0); // Palette color count.
 sink.writeIntLe(0); // 0 important colors.
 
 // Pixel data.
 for (int y = height - 1; y >= 0; y--) {
 int[] row = pixels[y];
 for (int x = 0; x < width; x++) {
 int pixel = row[x];
 sink.writeByte((pixel & 0x0000ff)); // Blue.
 sink.writeByte((pixel & 0x00ff00) >>> 8); // Green.
 sink.writeByte((pixel & 0xff0000) >>> 16); // Red.
 }
 
 // Padding for 4-byte alignment.
 for (int p = rowByteCountWithoutPadding; p < rowByteCount; p++) {
 sink.writeByte(0);
 }
 }
 }


Slide 136

Slide 136 text

sink.writeIntLe(0); // Palette color count.
 sink.writeIntLe(0); // 0 important colors.
 
 // Pixel data.
 for (int y = height - 1; y >= 0; y--) {
 int[] row = pixels[y];
 for (int x = 0; x < width; x++) {
 int pixel = row[x];
 sink.writeByte((pixel & 0x0000ff)); // Blue.
 sink.writeByte((pixel & 0x00ff00) >>> 8); // Green.
 sink.writeByte((pixel & 0xff0000) >>> 16); // Red.
 }
 
 // Padding for 4-byte alignment.
 for (int p = rowByteCountWithoutPadding; p < rowByteCount; p++) {
 sink.writeByte(0);
 }
 }
 }


Slide 137

Slide 137 text

(pixel & 0xff0000) >>> 16 • Shifting and masking lets you access the bits within an integer • & and | operators treat each int like a 32-element boolean array! • <<, >> and >>> operators slide bits left and right

Slide 138

Slide 138 text

1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0000ab00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 00ffab40 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 00ffab40 0000ff00 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0x00ffab40 & 0x0000ff00

Slide 139

Slide 139 text

1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0000ab00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 00ffab40 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 00ffab40 0000ff00 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0x00ffab40 & 0x0000ff00

Slide 140

Slide 140 text

1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0000ab00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 00ffab40 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 00ffab40 0000ff00 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0x00ffab40 & 0x0000ff00 & =

Slide 141

Slide 141 text

1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00000ab00 0x0000ab00 >> 8

Slide 142

Slide 142 text

1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00000ab00 0x0000ab00 >> 8 { 8

Slide 143

Slide 143 text

1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 000000ab 0x0000ab00 >> 8

Slide 144

Slide 144 text

1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 000000ab 0x0000ab00 >> 8 = 0x000000ab

Slide 145

Slide 145 text

sink.writeIntLe(0); // Palette color count.
 sink.writeIntLe(0); // 0 important colors.
 
 // Pixel data.
 for (int y = height - 1; y >= 0; y--) {
 int[] row = pixels[y];
 for (int x = 0; x < width; x++) {
 int pixel = row[x];
 sink.writeByte((pixel & 0x0000ff)); // Blue.
 sink.writeByte((pixel & 0x00ff00) >>> 8); // Green.
 sink.writeByte((pixel & 0xff0000) >>> 16); // Red.
 }
 
 // Padding for 4-byte alignment.
 for (int p = rowByteCountWithoutPadding; p < rowByteCount; p++) {
 sink.writeByte(0);
 }
 }
 }


Slide 146

Slide 146 text

This space intentionally left blank

Slide 147

Slide 147 text

int dibHeaderSize = 40;
 
 // BMP Header
 sink.writeUtf8("BM"); // ID.
 sink.writeIntLe(bmpHeaderSize + dibHeaderSize + pixelDataSize); // File size.
 sink.writeShortLe(0); // Unused.
 sink.writeShortLe(0); // Unused.
 sink.writeIntLe(bmpHeaderSize + dibHeaderSize); // Offset of pixel data.
 
 // DIB Header
 sink.writeIntLe(dibHeaderSize);
 sink.writeIntLe(width);
 sink.writeIntLe(height);
 sink.writeShortLe(1); // Color plane count.
 sink.writeShortLe(bytesPerPixel * Byte.SIZE);
 sink.writeIntLe(0); // No compression.
 sink.writeIntLe(16); // Size of bitmap data including padding.
 sink.writeIntLe(2835); // Horizontal print resolution in pixels/meter. (72 dpi).
 sink.writeIntLe(2835); // Vertical print resolution in pixels/meter. (72 dpi).
 sink.writeIntLe(0); // Palette color count.
 sink.writeIntLe(0); // 0 important colors.
 
 // Pixel data.


Slide 148

Slide 148 text

0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 0 0 0 Big Endian

Slide 149

Slide 149 text

0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Big Endian

Slide 150

Slide 150 text

0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 Big Endian

Slide 151

Slide 151 text

0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 0 0 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 Big Endian

Slide 152

Slide 152 text

0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 Big Endian

Slide 153

Slide 153 text

0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 Big Endian

Slide 154

Slide 154 text

0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 0 0 0 Little Endian

Slide 155

Slide 155 text

0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 0 0 0 Little Endian

Slide 156

Slide 156 text

1 0 1 0 1 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 0 0 0 Little Endian

Slide 157

Slide 157 text

1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 0 0 0 Little Endian

Slide 158

Slide 158 text

1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 0 0 0 Little Endian

Slide 159

Slide 159 text

1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 0 0 0 Little Endian

Slide 160

Slide 160 text

Data Models

Slide 161

Slide 161 text

Conference App! • Each talk has an ID, date, room, title, and speaker

Slide 162

Slide 162 text

JSON is Good Stuff {
 "id": 72017,
 "date": "2016-09-30T18:30:00Z",
 "room": "RIGHT",
 "title": "Decoding the Secrets of Binary Data",
 "speaker": "Jesse Wilson"
 }

Slide 163

Slide 163 text

JSON is Good Stuff {
 "id": 72017,
 "date": "2016-09-30T18:30:00Z",
 "room": "RIGHT",
 "title": "Decoding the Secrets of Binary Data",
 "speaker": "Jesse Wilson"
 } • A nice format that builds on UTF-8 • Easy to read & write

Slide 164

Slide 164 text

JSON is Self-Delimiting {
 "id": 72017,
 "date": "2016-09-30T18:30:00Z",
 "room": "RIGHT",
 "title": "Decoding the Secrets of Binary Data",
 "speaker": "Jesse Wilson"
 } • A JSON document has both structure and data • Uses escape sequences like \" to be completely unambiguous

Slide 165

Slide 165 text

"2016-09-30T18:30:00Z" • System.currentTimeMillis() returns milliseconds since January 1, 1970 at 00:00:00 UTC • A date that’s 8 bytes in memory is 22 bytes in JSON!

Slide 166

Slide 166 text

• Space: a simple message like this one is ~128 bytes • Time: bigger sequences take longer to decode JSON Space & Time

Slide 167

Slide 167 text

• Google’s “small, fast, simple” structured data format • Upon closer inspection, it’s not that different from JSON! • But it has a schema Protocol Buffers

Slide 168

Slide 168 text

{ message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }
 } "id": 72017, "date": "2016-09-30T18:30:00Z", "room": "RIGHT", "title": "Decoding the Secrets of Binary Data", "speaker": "Jesse Wilson" } "id" "date" "room" "title" "speaker" 72017 "2016-09-30T18:30:00Z", "RIGHT", "Decoding the Secrets of Binary Data", "Jesse Wilson",

Slide 169

Slide 169 text

message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }
 }

Slide 170

Slide 170 text

message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }
 } 000 enum, int32, int64... 001 fixed64 010 string, message 101 fixed32 Length Mode

Slide 171

Slide 171 text

message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }
 } 000 enum, int32, int64... 001 fixed64 010 string, message 101 fixed32 Length Mode 1 0 1

Slide 172

Slide 172 text

message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }
 } 000 enum, int32, int64... 001 fixed64 010 string, message 101 fixed32 Length Mode 1 0 1 0 0 1

Slide 173

Slide 173 text

message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }
 } 000 enum, int32, int64... 001 fixed64 010 string, message 101 fixed32 Length Mode 0 0 0 1 0 1 0 0 1

Slide 174

Slide 174 text

message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }
 } 000 enum, int32, int64... 001 fixed64 010 string, message 101 fixed32 Length Mode 0 0 0 1 0 1 0 1 0 0 0 1

Slide 175

Slide 175 text

message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }
 } 000 enum, int32, int64... 001 fixed64 010 string, message 101 fixed32 Length Mode 0 0 0 1 0 1 0 1 0 0 1 0 0 0 1

Slide 176

Slide 176 text

message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }
 } 0 0 0 1 0 1 0 1 0 0 1 0 0 0 1

Slide 177

Slide 177 text

message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }
 } 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 0 0 0 1

Slide 178

Slide 178 text

message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }
 } 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 0 0 0 0 1 0 0 0 1

Slide 179

Slide 179 text

message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }
 } 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 0 0 0 0 1 0 0 0 1

Slide 180

Slide 180 text

message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }
 } 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 1

Slide 181

Slide 181 text

message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }
 } 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 1 0 0 1 0 1 0 1 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 1

Slide 182

Slide 182 text

message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }
 } 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 1 0 0 1 0 1 0 1 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 1

Slide 183

Slide 183 text

message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }
 } 0d 11 18 22 2a

Slide 184

Slide 184 text

message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }a
 }a 0d 11 18 22 2a { "id": 72017, "date": "2016-09-30T18:30:00Z", "room": "RIGHT", "title": "Decoding the Secrets of Binary Data", "speaker": "Jesse Wilson" } "id" "date" "room" "title" "speaker" 72017 "2016-09-30T18:30:00Z", "RIGHT", "Decoding the Secrets of Binary Data", "Jesse Wilson",

Slide 185

Slide 185 text

message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }a
 }a 0d 11 18 22 2a 72017 "2016-09-30T18:30:00Z", "RIGHT", "Decoding the Secrets of Binary Data", "Jesse Wilson",

Slide 186

Slide 186 text

message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }a
 }a 0d 11 18 22 2a 72017 "2016-09-30T18:30:00Z", "RIGHT", "Decoding the Secrets of Binary Data", "Jesse Wilson",

Slide 187

Slide 187 text

51 19 01 message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }a
 }a 0d 11 18 22 2a "2016-09-30T18:30:00Z", "RIGHT", "Decoding the Secrets of Binary Data", "Jesse Wilson", 00

Slide 188

Slide 188 text

511901 message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }a
 }a 0d 11 18 22 2a "2016-09-30T18:30:00Z", "RIGHT", "Decoding the Secrets of Binary Data", "Jesse Wilson", 00

Slide 189

Slide 189 text

511901 message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }a
 }a 0d 11 18 22 2a "2016-09-30T18:30:00Z", "RIGHT", "Decoding the Secrets of Binary Data", "Jesse Wilson", 00

Slide 190

Slide 190 text

511901 message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }a
 }a 0d 11 18 22 2a "RIGHT", "Decoding the Secrets of Binary Data", "Jesse Wilson", 00 000001577D37EE40

Slide 191

Slide 191 text

511901 message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }a
 }a 0d 11 18 22 2a "RIGHT", "Decoding the Secrets of Binary Data", "Jesse Wilson", 00 00 00 01 57 7D 37 EE 40

Slide 192

Slide 192 text

511901 message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }a
 }a 0d 11 18 22 2a "RIGHT", "Decoding the Secrets of Binary Data", "Jesse Wilson", 00 00 00 01 57 7D 37 EE 40

Slide 193

Slide 193 text

511901 message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }a
 }a 0d 11 18 22 2a "Decoding the Secrets of Binary Data", "Jesse Wilson", 00 00 00 01 57 7D 37 EE 40 02

Slide 194

Slide 194 text

511901 message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }a
 }a 0d 11 18 22 2a "Decoding the Secrets of Binary Data" "Jesse Wilson", 00 00 01 57 7D 37 EE 40 02 00

Slide 195

Slide 195 text

511901 message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }a
 }a 0d 11 18 22 2a 4465636f64696e67207468652053656372657473206f662042696e6172792044617461 "Jesse Wilson", 00 00 00 01 57 7D 37 EE 40 02

Slide 196

Slide 196 text

511901 message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }a
 }a 0d 11 18 22 2a 4465636f64696e67207468652053656372657473206f662042696e6172792044617461 "Jesse Wilson", 00 00 00 01 57 7D 37 EE 40 02 23

Slide 197

Slide 197 text

511901 message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }a
 }a 0d 11 18 22 2a 4465636f64696e67207468652053656372657473206f662042696e6172792044617461 "Jesse Wilson" 00 00 00 01 57 7D 37 EE 40 02 23

Slide 198

Slide 198 text

511901 message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }a
 }a 0d 11 18 22 2a 4465636f64696e67207468652053656372657473206f662042696e6172792044617461 4a657373652057696c736f6e 00 00 00 01 57 7D 37 EE 40 02 23

Slide 199

Slide 199 text

511901 message Talk {
 optional fixed32 id = 1;
 optional fixed64 date = 2;
 optional Room room = 3;
 optional string title = 4;
 optional string speaker = 5;
 
 enum Room {
 UP = 1; RIGHT = 2; DOWN = 3; LEFT = 4;
 }a
 }a 0d 11 18 22 2a 4465636f64696e67207468652053656372657473206f662042696e6172792044617461 4a657373652057696c736f6e 00 00 00 01 57 7D 37 EE 40 02 23 0c

Slide 200

Slide 200 text

511901 0d 11 18 22 2a 4a657373652057696c736f6e 00 00 00 01 57 7D 37 EE 40 02 23 0c 4465636f64696e67207468652053656372657473206f662042696e6172792044617461

Slide 201

Slide 201 text

511901 0d 11 18 22 2a 4465636f64696e6720746865205365637265747 4a657373652057696c736f6e 00 00 00 01 57 7D 37 EE 40 02 23 0c 3206f662042696e6172792044617461

Slide 202

Slide 202 text

511901 0d 11 18 22 2a 4465636f64696e6720746865205365637265747 4a657373652057696c736f6e 00 00 00 01 57 7D 37 EE 40 02 23 0c 3206f662042696e6172792044617461

Slide 203

Slide 203 text

511901 0d 11 18 22 2a 4465636f64696e6720746865205365637265747 4a657373652057696c736f6e 00 00 00 01 57 7D 37 EE 40 02 23 0c 3206f662042696e6172792044617461 • Protocol Buffers are small and fast • ~1 byte for each field name • Compact encoding for numbers and enums • Strings stay the same length! • This message is 67 bytes in protocol buffers, vs. 128 for JSON

Slide 204

Slide 204 text

Java I/O

Slide 205

Slide 205 text

• Mutable! • equals() doesn’t work like it should: can’t be a map key! • Doesn’t implement Comparable byte[] is bad

Slide 206

Slide 206 text

• Range is -128..127 but you usually want 0..255 • But unsigned when you call InputStream.read() byte is signed

Slide 207

Slide 207 text

• Range is -128..127 but you usually want 0..255 • But unsigned when you call InputStream.read() byte is signed

Slide 208

Slide 208 text

Okio

Slide 209

Slide 209 text

Okio • Square’s library for efficient I/O • Used by OkHttp, Retrofit, Wire, Moshi

Slide 210

Slide 210 text

• Immutable! • equals() works like it should • Implements Comparable ByteString is good

Slide 211

Slide 211 text

ByteString makes data easy ByteString byteString = ByteString.decodeHex("436166c3a920f09f8da9");
 assertThat(byteString.size()).isEqualTo(10);
 assertThat(byteString.getByte(0)).isEqualTo((byte) 0x43);
 assertThat(byteString.getByte(0)).isEqualTo((byte) 67);
 
 String cafeDonuts = byteString.utf8();
 assertThat(cafeDonuts).isEqualTo("Café ");
 
 ByteString cafe = ByteString.encodeUtf8("Café");
 assertThat(byteString.startsWith(cafe)).isTrue();

Slide 212

Slide 212 text

ByteString and Parcelable

Slide 213

Slide 213 text

class Talk implements Parcelable {
 public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
 @Override public Talk createFromParcel(Parcel in) {
 int id = in.readInt();
 long date = in.readLong();
 Room room = Room.values()[in.readInt()];
 String title = in.readString();
 String speaker = in.readString();
 return new Talk(id, date, room, title, speaker);
 }
 };
 …
 
 @Override public void writeToParcel(Parcel out, int flags) {
 out.writeInt(id);
 out.writeLong(date);
 out.writeInt(room.ordinal());
 out.writeString(title);
 out.writeString(speaker);
 }
 }

Slide 214

Slide 214 text

@Test public void decodeGolden() {
 Talk talk = new Talk(72017, 1475260200000L, Room.RIGHT,
 "Decoding the Secrets of Binary Data", "Jesse Wilson");
 ByteString goldenData = ByteString.decodeHex("01000000511901004034…");
 assertThat(parcelDecode(goldenData, Talk.CREATOR)).isEqualTo(talk);
 }a
 
 private T parcelDecode(
 ByteString byteString, Parcelable.Creator creator) {
 Parcel parcel = Parcel.obtain();
 try {
 parcel.unmarshall(byteString.toByteArray(), 0, byteString.size());
 parcel.setDataPosition(0);
 return parcel.readTypedObject(creator);
 } finally {
 parcel.recycle();
 }a
 }a

Slide 215

Slide 215 text

@Test public void decodeGolden() {
 Talk talk = new Talk(72017, 1475260200000L, Room.RIGHT,
 "Decoding the Secrets of Binary Data", "Jesse Wilson");
 ByteString goldenData = ByteString.decodeHex("01000000511901004034…");
 assertThat(parcelDecode(goldenData, Talk.CREATOR)).isEqualTo(talk);
 }a
 
 private T parcelDecode(
 ByteString byteString, Parcelable.Creator creator) {
 Parcel parcel = Parcel.obtain();
 try {
 parcel.unmarshall(byteString.toByteArray(), 0, byteString.size());
 parcel.setDataPosition(0);
 return parcel.readTypedObject(creator);
 } finally {
 parcel.recycle();
 }a
 }a

Slide 216

Slide 216 text

Next Steps

Slide 217

Slide 217 text

• Everything is bytes • Java Strings are UTF-16. Encoded text is usually UTF-8 • Hex is handy • Don’t be afraid of shifting and masking • Integers are big or little-endian • Java’s I/O APIs are trouble 6 tips

Slide 218

Slide 218 text

• You can encode almost anything • Okio makes it easy No Black Boxes

Slide 219

Slide 219 text

@jessewilson https://github.com/swankjesse/encoding Thanks!