|
| 1 | +# Encoji encoding standard |
| 2 | + |
| 3 | +Encoji maps input data into 1024 Unicode emojis (plus 5 padding emojis). Ten |
| 4 | +bits are needed to represent 1024. Ecoji reads 5 bytes at a time because this |
| 5 | +is 40 bits which is a multiple of 10. For each 5 bytes read, 4 emojis are |
| 6 | +output. When less than 5 bytes are available, special padding emojis are |
| 7 | +output. In [mapping.go](../mapping.go) the 1024 emojis and the padding emojis |
| 8 | +are defined. These same emojis should be used in other languages. |
| 9 | + |
| 10 | +Below is some pseudo code for translating bytes to emojis. Also see [encode.go](../encode.go). |
| 11 | + |
| 12 | +```java |
| 13 | + |
| 14 | +Input input; //input data, read 5 bytes at a time from |
| 15 | +Output output; // where unicode emojis are written to |
| 16 | +byte data[5]; //buffer that bytes are read into |
| 17 | +int numRead; |
| 18 | + |
| 19 | +//assumed this reads maximum available data up to five bytes |
| 20 | +while ((numRead = input.read(data)) > 0) { |
| 21 | + for(int i = numRead; i < 5; i++) { |
| 22 | + //zero out unread data |
| 23 | + data[i] = 0; |
| 24 | + } |
| 25 | + |
| 26 | + switch (numRead) { |
| 27 | + case 1: |
| 28 | + output.writeUnicode(emojis[data[0]<<2 | data[1]>>6]); |
| 29 | + output.writeUnicode(padding); |
| 30 | + output.writeUnicode(padding); |
| 31 | + output.writeUnicode(padding); |
| 32 | + break; |
| 33 | + case 2: |
| 34 | + output.writeUnicode(emojis[data[0]<<2 | data[1]>>6]); |
| 35 | + output.writeUnicode(emojis[(data[1] & 0x3f)<<4 | data[2]>>4]); |
| 36 | + output.writeUnicode(padding); |
| 37 | + output.writeUnicode(padding); |
| 38 | + break; |
| 39 | + case 3: |
| 40 | + output.writeUnicode(emojis[data[0]<<2 | data[1]>>6]); |
| 41 | + output.writeUnicode(emojis[(data[1] & 0x3f)<<4 | data[2]>>4]); |
| 42 | + output.writeUnicode(emojis[(data[2] & 0x0f)<<6 | data[3]>>2]); |
| 43 | + output.writeUnicode(padding); |
| 44 | + break; |
| 45 | + case 4: |
| 46 | + output.writeUnicode(emojis[data[0]<<2 | data[1]>>6]); |
| 47 | + output.writeUnicode(emojis[(data[1] & 0x3f)<<4 | data[2]>>4]); |
| 48 | + output.writeUnicode(emojis[(data[2] & 0x0f)<<6 | data[3]>>2]); |
| 49 | + |
| 50 | + //look at last two bits of 4th byte to determine padding to use |
| 51 | + switch (data[3] & 0x03) { |
| 52 | + case 0: |
| 53 | + output.writeUnicode(padding40); |
| 54 | + break; |
| 55 | + case 1: |
| 56 | + output.writeUnicode(padding41); |
| 57 | + break; |
| 58 | + case 2: |
| 59 | + output.writeUnicode(padding42); |
| 60 | + break; |
| 61 | + case 3: |
| 62 | + output.writeUnicode(padding43); |
| 63 | + break; |
| 64 | + } |
| 65 | + break; |
| 66 | + |
| 67 | + case 5: |
| 68 | + // use 8 bits from 1st byte and 2 bits from 2nd byte to lookup emoji |
| 69 | + output.writeUnicode(emojis[data[0]<<2 | data[1]>>6]); |
| 70 | + // use 6 bits from 2nd byte and 4 bits from 3rd byte to lookup emoji |
| 71 | + output.writeUnicode(emojis[(data[1] & 0x3f)<<4 | data[2]>>4]); |
| 72 | + // use 4 bits from 3rd byte and 6 bits from 4th byte to lookup emoji |
| 73 | + output.writeUnicode(emojis[(data[2] & 0x0f)<<6 | data[3]>>2]); |
| 74 | + //user 2 bits from 4th byte and 8 bits from 5th byte to lookup emoji |
| 75 | + output.writeUnicode(emojis[(data[3] & 0x03)<<8 | data[4]]); |
| 76 | + break; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +``` |
| 81 | + |
| 82 | +For decoding, see [decode.go](../decode.go). The code needs to be cleaned up, it was written while learning Go. |
| 83 | + |
| 84 | + |
0 commit comments