Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- text to quacks encoder/decoder
- version 1
- by KKosty4ka
- use the encodeText and decodeText functions
- (or the encodeBytes and decodeBytes if you are crazy enough to encode byte arrays with this)
- */
- function encodePunctuation(b5, b6, b7)
- {
- var output = "";
- if (b5 && b6) output = "?";
- else if (b5 && !b6) output = "!";
- else if (!b5 && b6) output = "?!";
- if (b7) output = output.padEnd(output.length === 2 ? 4 : 3, ".");
- return output;
- }
- function encodeWord(b0, b1, b2, b3, b4)
- {
- var output = "";
- output += b0 ? "Q" : "q";
- output += b1 ? "U" : "u";
- output += b2 ? "A" : "a";
- output += b3 ? "C" : "c";
- output += b4 ? "K" : "k";
- return output;
- }
- function encodeByte(byte)
- {
- return encodeWord(
- byte & 0b00000001,
- byte & 0b00000010,
- byte & 0b00000100,
- byte & 0b00001000,
- byte & 0b00010000
- ) + encodePunctuation(
- byte & 0b00100000,
- byte & 0b01000000,
- byte & 0b10000000
- );
- }
- function encodeBytes(bytes)
- {
- return [...bytes].map(encodeByte).join(" ");
- }
- function encodeText(text)
- {
- return encodeBytes(new TextEncoder().encode(text));
- }
- function decodePunctuation(text)
- {
- var b5 = 0;
- var b6 = 0;
- var b7 = +text.endsWith(".");
- if (text.startsWith("?!")) b6 = 1;
- else if (text.startsWith("!")) b5 = 1;
- else if (text.startsWith("?")) b5 = b6 = 1;
- return [b5, b6, b7];
- }
- function decodeWord(text)
- {
- var output = [];
- for (var i = 0; i < 5; i++)
- {
- output.push(+(text[i] === text[i].toUpperCase()));
- }
- return output;
- }
- function decodeByte(text)
- {
- var bits = decodeWord(text).concat(decodePunctuation(text.substring(5)));
- var output = 0;
- for (var i = 0; i < 8; i++)
- {
- output += bits[i] * (1 << i);
- }
- return output;
- }
- function decodeBytes(text)
- {
- return new Uint8Array(text.split(" ").map(decodeByte));
- }
- function decodeText(text)
- {
- return new TextDecoder().decode(decodeBytes(text));
- }
Advertisement
Comments
-
- i think that the quack can quack the quack but the quacks quack so quack quacks
Add Comment
Please, Sign In to add comment
Advertisement