Last active
January 28, 2016 19:27
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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 of01010100
in the ASCII system, or84
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 StringHello World.
would be treated as…NULNULNULHello World.
, never ending. Null bytes can be intentionally in Strings, and shouldn't be ignored, howeverThe solution in this system is for the integer calculator to add
(long)Math.pow(2,8*message.length())
to the message, essentially making it1MESSAGE
, 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 as11MESSAGE
. 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 towhile (message>newValue)
. I chose 1 because that is the minimum recognizable as greater than zero.