Skip to content

Instantly share code, notes, and snippets.

@EtherTyper
Last active January 28, 2016 19:27
Show Gist options
  • Save EtherTyper/4872465f24c4cb6bd513 to your computer and use it in GitHub Desktop.
Save EtherTyper/4872465f24c4cb6bd513 to your computer and use it in GitHub Desktop.
Cast up to five or six character Strings as longs. Works with null bytes.
public class StringToDec{
private StringToDec(){}
/* Note: Only supports ASCII, for simplification of the message encoded.
The software prefixes all codes with '1' so that the first letter
isn't truncated. For example: T evaluates as 340, not 84. The
entireity of the software is MIT License. To compile and run,
visit <http://openjdk.java.net/install/index.html>. Have fun!
*/
public static long calc(String message){
long encoded=1;
while (message.length()>0){
encoded*=(long)Math.pow(2,8);
encoded+=(long)(message.charAt(0)%Math.pow(2,8));
message=message.substring(1);
}
return encoded;
}
public static String calc(long message){
String decoded="";
while (message>1){
decoded+=(char)(message%Math.pow(2,8));
message/=(long)Math.pow(2,8);
}
decoded=new StringBuilder(decoded).reverse().toString();
return decoded;
}
}
import java.lang.*;
import java.util.*;
public class StringToDecTest{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
String message;
System.out.println("LONGS ARE INCAPABLE OF HOLDING LARGE FILES.\nDO NOT USE THIS PROGRAM TO ENCODE LONG MESSAGES OF IMPORTANCE.\n");
while (true){
System.out.print("String to encode: ");
message=input.nextLine();
System.out.println("Encoded as an integer: "+StringToDec.calc(message)+"\nDecoded again: "+StringToDec.calc(StringToDec.calc(message))+"\n");
}
}
}
@EtherTyper
Copy link
Author

Sample Test && Null bytes

The following test determines whether, in most situations, the software will function. It uses both calc methods to determine whether the software can accurately handle Strings and return them.

out.println(StringToDec.calc(StringToDec.calc("Does it work?")));

However, you may be wondering why the half-baked integer encodings appear with a prefix of "1" in binary. This is because of the integer's linear value system. Let us use T for an example, the equivalent of 01010100 in the ASCII system, or 84 in decimal. With our system reading the values from right to left, zeroes in the middle of the String, or even in the first character, aren't a problem. It reads through chunks of ASCII, and when it finds zeroes, it assumes the zeroes are part of the code. However, in this case, a String Hello World. would be treated as …NULNULNULHello World., never ending. Null bytes can be intentionally in Strings, and shouldn't be ignored, however

The solution in this system is for the integer calculator to add (long)Math.pow(2,8*message.length()) to the message, essentially making it 1MESSAGE, with the one signifying that the String calculator should stop there. If a 1 is actually desirable as the first character, the message will simply encode as 11MESSAGE. Because we can test "size" in number form, the loop will see the "larger", or earlier, one and wait for it to die. Overall, I hope you fellow scholars appreciate this String to long casting as much as I did! Thanks!

NOTE: Choosing different values for end signatures can be performed by changing the first line of the integer calculator to long encoded=newValue; to change the prefix it signs the integers with, and the while loop signature in the String calculator to while (message>newValue). I chose 1 because that is the minimum recognizable as greater than zero.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment