Skip to content

Instantly share code, notes, and snippets.

@WOSAJ
Created March 9, 2022 17:54
Show Gist options
  • Save WOSAJ/f8ab8ab13c1025d90de342a2fa413451 to your computer and use it in GitHub Desktop.
Save WOSAJ/f8ab8ab13c1025d90de342a2fa413451 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
final String help = "WOSAJ 2022, all rights recieved\n"
+"+===WOSAJ'S DECODE HELP===+\n"
+"en [text] - encode text\n"
+"de [text] - decode text\n"
+"ende [text] - encode text and decode encoded text\n"
+"br - enable/disable braces(for see spaces)\n"
+"ex - exit";
System.out.println(help);
boolean braces = false;
var s = new java.util.Scanner(System.in);
while(true){
System.out.print('>');
var ss = s.nextLine();
if(ss.trim().startsWith("en ")) {
var sss = ss.substring(3);
if(braces)
System.out.printf(">%s<\n", encode(sss));
else
System.out.println(encode(sss));
continue;
} else if(ss.trim().startsWith("de ")) {
var sss = ss.substring(3);
if(braces)
System.out.printf(">%s<\n", decode(sss));
else
System.out.println(decode(sss));
continue;
} else if(ss.trim().startsWith("ende ")) {
var sss = ss.substring(5);
System.out.printf("Coded: >%s<\nDecoded: >%s<\n", encode(sss), decode(encode(sss)));
continue;
} if(ss.trim().equals("br")) {
braces = !braces;
if(braces) System.out.println("Braces enabled");
else System.out.println("Braces disabled");
continue;
}else if(ss.trim().equals("help")) {
System.out.println(help);
continue;
} else if(ss.trim().equals("ex")) {
break;
} else System.err.println("Invalid command! Enter 'help' for help");
}
s.close();
}
public static Boolean[] byteToBoolArr(Byte b) {
Boolean boolArr[] = new Boolean[8];
for(int i=0;i<8;i++) boolArr[i] = (b & (byte)(128 / Math.pow(2, i))) != 0;
return boolArr;
}
public static Byte boolArrToByte(Boolean[] bool) {
return (byte)((bool[0]?1<<7:0) + (bool[1]?1<<6:0) + (bool[2]?1<<5:0) +
(bool[3]?1<<4:0) + (bool[4]?1<<3:0) + (bool[5]?1<<2:0) +
(bool[6]?1<<1:0) + (bool[7]?1:0));
}
public static Byte[] wrap(byte[] in) {
var l = new Byte[in.length];
for(int i=0;i<in.length;i++) l[i] = in[i];
return l;
}
public static byte[] unwrap(Byte[] in) {
var l = new byte[in.length];
for(int i=0;i<in.length;i++) l[i] = in[i];
return l;
}
public static String encode(String in) {
var bytes = Arrays.asList(wrap(in.getBytes()));
var bools = new ArrayList<Boolean[]>();
bytes.forEach(b -> bools.add(byteToBoolArr(b)));
var heads = bools.stream().map(b -> b[7]).collect(Collectors.toList());
Collections.rotate(heads, 1);
for(int i=0;i<bools.size();i++){
bools.get(i)[7]=heads.get(i);
bools.get(i)[4]=!bools.get(i)[4];
bools.get(i)[3]=!bools.get(i)[3];
//bools.get(i)[1]=!bools.get(i)[1];
}
var po = bools.stream().map(b -> boolArrToByte(b)).toArray(Byte[]::new);
return new String(unwrap(po));
}
public static String decode(String in) {
var bytes = Arrays.asList(wrap(in.getBytes()));
var bools = new ArrayList<Boolean[]>();
bytes.forEach(b -> bools.add(byteToBoolArr(b)));
var heads = bools.stream().map(b -> b[7]).collect(Collectors.toList());
Collections.rotate(heads, -1);
for(int i=0;i<bools.size();i++){
//bools.get(i)[1]=!bools.get(i)[1];
bools.get(i)[3]=!bools.get(i)[3];
bools.get(i)[4]=!bools.get(i)[4];
bools.get(i)[7]=heads.get(i);
}
var po = bools.stream().map(b -> boolArrToByte(b)).toArray(Byte[]::new);
return new String(unwrap(po));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment