Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

java - From random integers to an actual String

This is a code that takes in an array of all the printing characters of the ASCII table. I am trying to make it so that any String message in the form of integers (e.g. String "aba" that is converted 97098097 can be put back into its original String form. 100101101 can be taken and made back into "dee". I've really tried hard with this method but it does not seem to be working, especially when it comes to numbers and such please help me. It is in Java by the way and I am using Eclipse.

public static String IntToString (){


int n = 0;
String message = "";
String message2 = null;
String [] ASCII = {" ","!",""","#","$","%","&","'","(",")","*","+",",","-",".","/","0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?","@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[","","]","^","_","`","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","{","|","}","~"};
String IntMessage = result.toString();
String firstChar = IntMessage.substring(0,2);
if (IntMessage.substring(0,1)=="1" && IntMessage.length()%3==0)
{
    for (int x = (IntMessage.length() % 3 - 3) % 3; x < IntMessage.length()-2; x += 3)
        n = Integer.parseInt(IntMessage.substring(Math.max(x, 0), x + 3));
        message=message.concat(ASCII[n-31]);
return message;
}
else if (IntMessage.length()%3==2)
message2=ASCII[(Integer.parseInt(firstChar))-31];
        for (int x = 2; x < IntMessage.length()-2; x += 3)
            n = Integer.parseInt(IntMessage.substring(x, x + 3));
            message=message2+=ASCII [n - 31];
return message;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It would seem that your encoding scheme is, er, crazy.

First, you take the ASCII value of a string, then take the character representation of that ASCII value, then store it as a string.

So "abc" => {97, 98, 99} => "979899".

But since you are using ASCII, which can have values of 100 or more, you are padding your ints with 0 if they are under 100:

"abc" => {97, 98, 99} => {"097", "098", "099"} => "097098099"

But you decide to do this only sometimes, because somehow

"aba" => "97098097"

That is, the first "a" is turned into "97", but the last "a" is turned into "097".

I'd say you should fix your encoding scheme first.

Also, these are hopefully not "random integers" because you are trying to turn them into sensible strings. Otherwise a simple mapping such as base64 would easily map any integers to strings, they just might not make much sense.

In fact, they aren't even really integers. You're storing your encoded strings as strings.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...