dec binary hex oct 65 == 01000001 == 0x41 == 0101
0 int: 00000000 00000000 00000000 00000000 byte: 00000000 -1 int: 11111111 11111111 11111111 11111111 byte: 11111111 127 int: 00000000 00000000 00000000 01111111 byte: 01111111 -128 int: 11111111 11111111 11111111 10000000 byte: 10000000 128 int: 00000000 00000000 00000000 10000000 byte: --
ASCII (or Unicode UTF-8)
ltr binary hex dec 'A' 01000001 0x41 65 'a' 01100001 0x61 97 'b' 01100010 0x62 98 '0' 00110000 0x30 48 '1' 00110001 0x31 49 '2' 00110010 0x32 50 '3' 00110011 0x33 51
8 bits per byte; 1 byte = 1 symbol; 256 possible symbols.
int ltr = 'A'; System.out.println(ltr); //prints 65
'0' 0000 '1' 0001 '2' 0010 '3' 0011 ... '9' 1001 'A' 1100 'a' 1101 'b' 1110
Now only need 4 bits per symbol. More efficient encoding!
A 0 0 10 1 110 2 111000 3 111001 4 111010 ... 9 111111
Example (symbol:count): A:32, 0:16, 1:8, 2:1, 3:1, 4:1, 5:1, 6:1; 7:1, 8:1, 9:1.
(Note: tree can vary depending on your policy of child order as they come out of PQ)
You try. Build tree from: a:7, b:5, c:4, d:2, e:1.
What are the paths (0 = left, 1 = right) to each node?
create a stack read first 0 from stream set root to a new empty internal node and push it onto stack while stack is not empty: read next bit from stream if bit is 0: create a new empty internal node else on a 1: read 8 more bits and create a new leaf node with the data if stack.peek() does not have left child: add the new node as left child of stack.peek() else: add the new node as right child of stack.peek() pop the top of the stack (because now it has two children) if the new node is an internal node: push the new node onto stack
You try: 01[c]01[d]1[b]
String filename = ... try { FileInputStream file = new FileInputStream(filename); int val = file.read(); if (val == -1) { //reached end of file }else { byte b = (byte) val; //one way to convert } }catch (IOException e) { System.out.println("Could not read from file: " + e.getMessage()); }
[Out of time; will return to this on Wed.]
byte[] data = {1, 2, 3, 4}; int together = 0; for (byte b : data) { together = (together << 8) | b; } System.out.println(together); //0x01020304 = 16909060
byte b = '3'; // or 51 or 0x33 for (int i = 0; i < 8; i++) { //prints: 00110011 int mask = 0x0080 >> i; int bit = b & mask; bit = bit >> (7 - i); System.out.print(bit); } System.out.println();